【发布时间】:2026-02-13 00:05:01
【问题描述】:
我正在尝试实现一个基本的模板元编程结构,它使用std::is_same 确定类型列表是否都相同。我尝试按如下方式实现它:
template <typename T, typename U, typename... Args>
struct check_same {
static const bool value = std::is_same<T, U>::value && check_same<U, Args...>::value;
};
template <typename T, typename U>
struct check_same {
static const bool value = std::is_same<T, U>::value;
};
但是,如果我尝试实例化 check_same,则会收到以下编译器错误:
'check_same' : 模板参数太少
为什么这不是执行编译时布尔代数的有效方法?当然,所有涉及的表达式都是constexpr(或const,因为MSVC 还没有实现constexpr),它应该编译吗?
以下将无法编译:
int main()
{
static_assert( check_same<int, unsigned int, float>::value, "Types must be the same" );
return 0;
}
【问题讨论】:
-
您缺少
value的类型。不确定这与您的错误有什么关系。 -
“模板参数太少”,因为 Args 被此递归消耗到 0,并且没有 check_same 的强制参数少于 2 个。
标签: c++ templates c++11 template-meta-programming