Skip to content

Instantly share code, notes, and snippets.

@CyberDNIWE
Created November 21, 2022 17:41
Show Gist options
  • Save CyberDNIWE/293fc5e755203cf63f7d65fe0bc075fa to your computer and use it in GitHub Desktop.
Save CyberDNIWE/293fc5e755203cf63f7d65fe0bc075fa to your computer and use it in GitHub Desktop.
poorly::static_max
// Poorly::static_max allows recursively calculating maximum value from size_t parameters/-packs at compile time
// Easily editable for any concrete parameters by replacing size_t to your desired type
namespace poorly
{
// Originally taken from: ( https://gist.github.com/tibordp/6909880 )
// Usage:
// template<typename Ts...>
// struct MyStruct { const size_t data_size = static_max<sizeof(Ts)...>::value };
template <size_t arg>
struct static_max<arg>
{
static const size_t value = arg;
};
template <size_t arg1, size_t arg2, size_t ... others>
struct static_max<arg1, arg2, others...>
{
static const size_t value = arg1 >= arg2 ? static_max<arg1, others...>::value :
static_max<arg2, others...>::value;
};
};
// Constexpr-friendly version
namespace poorly
{
// Usage:
// template<typename Ts...>
// struct MyStruct { const size_t data_size = static_max<sizeof(Ts)...>::value() };
template <size_t arg1, size_t ... others>
struct static_max;
template <size_t arg>
struct static_max<arg>
{
static constexpr size_t value() noexcept { return arg; }
};
template <size_t arg1, size_t arg2, size_t ... others>
struct static_max<arg1, arg2, others...>
{
static constexpr size_t value() noexcept
{
return arg1 >= arg2 ? static_max<arg1, others...>::value() : static_max<arg2, others...>::value();
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment