mardi 5 septembre 2017

Max sizeof metafunction in variadic templates

I'm trying to implement a metafunction(?) in variadic templates to calculate the max of sizeof of a few types at compile-time.

template<typename... Ts> struct MaxSizeof {
  constexpr size_t value = 0;
};

template<typename T, typename... Ts> struct MaxSizeof {
  constexpr size_t value = std::max(sizeof(T), typename MaxSizeof<Ts...>::value);
};

But I'm getting a few strange errors:

MaxSizeof.h(2): error C2126: 'MaxSizeof<Ts...>::value' cannot be declared with 'constexpr' specifier
MaxSizeof.h(3): note: see reference to class template instantiation 'MaxSizeof<Ts...>' being compiled
MaxSizeof.h(7): error C3855: 'MaxSizeof': template parameter 'Ts' is incompatible with the declaration
MaxSizeof.h(7): error C2977: 'MaxSizeof': too many template arguments
MaxSizeof.h(5): note: see declaration of 'MaxSizeof'

If I change from constexpr to const, I still get strange compiler errors.

Could you help fixing my code?

The compiler is MSVC++2017 toolset v141.

Aucun commentaire:

Enregistrer un commentaire