Skip to content

Instantly share code, notes, and snippets.

@Bktero
Created March 7, 2018 09:55
Show Gist options
  • Save Bktero/91f2a370f3fc9531212f3018ea56bf67 to your computer and use it in GitHub Desktop.
Save Bktero/91f2a370f3fc9531212f3018ea56bf67 to your computer and use it in GitHub Desktop.
[C++14] Get the size of the largest type out of a list of types at compile time
#include <iostream>
#include "MaxSize.hpp"
int main() {
std::cout << Max<char>::value << std::endl;
std::cout << Max<char, short>::value << std::endl;
std::cout << Max<char, short, int>::value << std::endl;
std::cout << Max<int, short, char>::value << std::endl;
std::cout << Max<int, float, double>::value << std::endl;
}
#ifndef MAXSIZE_HPP_
#define MAXSIZE_HPP_
#include <algorithm>
#include <cstddef>
template<class ... Ts>
struct Max {
static_assert(sizeof...(Ts) == 0, "This is the terminal case of the recursion, this assertion shall never fail");
static constexpr std::size_t value = 0;
};
template<class T, class ... Ts>
struct Max<T, Ts...> : Max<Ts...> {
static constexpr std::size_t value = std::max(sizeof(T), Max<Ts...>::value);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment