此代码
#include <iostream>
#include <type_traits>
template<typename Head, typename... Tail>
struct Is_Admitted {
constexpr static bool value = Is_Admitted<Head>::value && Is_Admitted<Tail...>::value;
};
template<>
template<typename T>
struct Is_Admitted<T> : public std::false_type{};
template<> struct Is_Admitted<int> : public std::true_type{};
template<> struct Is_Admitted<double> : public std::true_type{};
int main()
{
std::cout << Is_Admitted<int, double>::value << '\n';
std::cout << Is_Admitted<int, char>::value << '\n';
}
(错误描述由我自己翻译,因为我无法将英语设置为编译语言,因此它们可能与原始语言不匹配)
error C2910: 'Is_Admitted<T,>': impossible to perform explicit specialization
error C2065: 'value': undeclared identifier
error C2976: 'Is_Admitted': insufficients template arguments
error C2131: constant expression does not return any value
哪个编译器正确,哪个编译器错了?该代码是否符合c ++ 11,c ++ 14或c ++ 17标准?
什么是我正在尝试做的正确方法,这是一个可变类型函数,只有当所有模板类型参数都是某些被允许的类型时才返回true?
答案 0 :(得分:1)
这里有一个额外的template<>
:
template<> // <===
template<typename T>
struct Is_Admitted<T> : public std::false_type{};
您的代码通过webcompiler向我提供了相同的错误。 简单地删除它,它编译得很好。我不明白这是如何编译gcc或clang。
只有在您定义类定义之外的类模板的成员模板时才需要两个template
声明,例如:
template <typename T>
struct Foo {
template <typename U>
void bar();
};
template <typename T>
template <typename U>
void Foo<T>::bar() { ... }