Last active
September 15, 2019 17:50
-
-
Save wayslog/f2c406c4217895a883c351d94d4da35c to your computer and use it in GitHub Desktop.
get max size of Type List in constexpr
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
#include <iostream> | |
struct A { | |
uint64_t f1; | |
}; | |
struct B { | |
uint64_t f1; | |
uint64_t f2; | |
}; | |
struct C { | |
uint64_t f1; | |
uint64_t f2; | |
uint64_t f3; | |
}; | |
template <typename T> | |
static constexpr T static_max(T a, T b) { | |
return a < b ? b : a; | |
} | |
template <typename T, typename... Ts> | |
static constexpr T static_max(T a, Ts... bs) { | |
return static_max(a, static_max(bs...)); | |
} | |
template <class... Types> | |
constexpr size_t max_size() { | |
return static_max(sizeof(Types)...); | |
}; | |
struct D { | |
char all[max_size<A, B, C>() * 3]; | |
}; | |
int main(int argc, char const* argv[]) { | |
std::cout << max_size<A, B, C>() << std::endl; | |
D d1; | |
std::cout << sizeof(d1) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment