VS2015:可变参数模板专业化
VS2015: Variadic template specialization
此代码
#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';
}
- 在 GCC 下编译和 运行 正常(>=4.7,启用 c++11、c++14 或 c++17 支持)
- 编译时出现警告,运行 正常 clang 3.6(启用 c++11、c++14 或 c++17 支持)
- 在 VS2015RC(或预览版)下不编译并出现以下错误:
(错误描述是我自己翻译成英文的,因为我无法将英文设置为编译语言,所以它们可能与原始的不匹配)
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 标准?
做我想做的事情的正确方法是什么,这是一个可变类型函数,returns 只有当所有模板类型参数都是某些允许的类型时才为真?
你这里有一个额外的 template<>
:
template<> // <===
template<typename T>
struct Is_Admitted<T> : public std::false_type{};
你的代码通过 webcompiler 给我同样的错误。
只需将其删除即可正常编译。我不明白这是如何在 gcc 或 clang 上编译的。
只有在 class 定义之外定义 class 模板的成员模板时才需要两个 template
声明,例如:
template <typename T>
struct Foo {
template <typename U>
void bar();
};
template <typename T>
template <typename U>
void Foo<T>::bar() { ... }
此代码
#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';
}
- 在 GCC 下编译和 运行 正常(>=4.7,启用 c++11、c++14 或 c++17 支持)
- 编译时出现警告,运行 正常 clang 3.6(启用 c++11、c++14 或 c++17 支持)
- 在 VS2015RC(或预览版)下不编译并出现以下错误:
(错误描述是我自己翻译成英文的,因为我无法将英文设置为编译语言,所以它们可能与原始的不匹配)
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 标准?
做我想做的事情的正确方法是什么,这是一个可变类型函数,returns 只有当所有模板类型参数都是某些允许的类型时才为真?
你这里有一个额外的 template<>
:
template<> // <===
template<typename T>
struct Is_Admitted<T> : public std::false_type{};
你的代码通过 webcompiler 给我同样的错误。 只需将其删除即可正常编译。我不明白这是如何在 gcc 或 clang 上编译的。
只有在 class 定义之外定义 class 模板的成员模板时才需要两个 template
声明,例如:
template <typename T>
struct Foo {
template <typename U>
void bar();
};
template <typename T>
template <typename U>
void Foo<T>::bar() { ... }