Created
September 15, 2021 18:33
-
-
Save valmat/9c3d3c14fa7695b35e56086be9140475 to your computer and use it in GitHub Desktop.
if constexpr to switch type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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