Skip to content

Instantly share code, notes, and snippets.

@valmat
Created September 15, 2021 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valmat/9c3d3c14fa7695b35e56086be9140475 to your computer and use it in GitHub Desktop.
Save valmat/9c3d3c14fa7695b35e56086be9140475 to your computer and use it in GitHub Desktop.
if constexpr to switch type
// if constexpr to switch type
//
template<uint8_t N>
struct _buf_type
{
static_assert(N > 0, "Buffer must be more then 0");
static_assert(N <= 64, "Maximum buffer size is 64");
constexpr static auto _i2t()
{
if constexpr (N <= 8 ) {return uint8_t(0) ;}
else if constexpr (N <= 16) {return uint16_t(0);}
else if constexpr (N <= 32) {return uint32_t(0);}
else if constexpr (N <= 64) {return uint64_t(0);}
}
public:
using type = decltype(_i2t());
};
// Using:
// using data_type = typename _buf_type<N>::type;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment