我正在尝试在可变参数模板中实现一个元函数(?),以在编译时计算几种类型的 sizeof
的最大值。
template<typename... Ts> struct MaxSizeof {
static constexpr size_t value = 0;
};
template<typename T, typename... Ts> struct MaxSizeof {
static constexpr size_t value = std::max(sizeof(T), typename MaxSizeof<Ts...>::value);
};
但是我遇到了一些奇怪的错误:
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'
你能帮助修复我的代码吗?
编译器为MSVC++2017 toolset v141。
最佳答案
您的特化语法不正确,应该是:
template<typename T, typename... Ts>
struct MaxSizeof<T, Ts...> { // Note the <T, Ts...> here
// ....
};
关于c++ - 可变参数模板中元函数的最大大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46059376/