我正在尝试为特定类型专门化可变参数模板类。
我正在努力实现这一目标:
template<typename... Ts>
class myclass
{
///...
};
template<>
class myclass<int... N>
{
///...
};
我得到了这个错误:
error C2760: syntax error: unexpected token 'int', expected 'expression'
error C2187: syntax error: '...' was unexpected here
error C2065: 'N': undeclared identifier
error C2913: explicit specialization; 'ex::vec' is not a specialization of a class templa
谁能提示我做错了什么?
答案 0 :(得分:1)
您的 myclass
是为模板参数的可变参数列表声明的
template<typename... Ts>
class myclass
您可以专门针对特定类型,而不是特定值,如您的问题(已更正语法,但对于类型/值问题仍然错误)
template <int N>
class myclass<N...>
如果您将值作为类的模板参数,则不同,例如
template <typename ... Ts>
class myclass
{ };
template <template <int...> class C, int ... Is>
class myclass<C<Is...>>
{ };
// ...
template <int ...>
class foo
{ };
// ...
myclass<foo<0, 1, 2>> m0; // uses specialization
答案 1 :(得分:1)
以下代码是解决您问题的方法,而不是直接的解决方案。
直接处理参数包可能非常棘手,所以让我们添加一个间接级别。您可以"wrap" the parameter pack 使用另一个模板,而不是尝试专门化您的可变参数模板:
#include <type_traits>
template <class... T>
struct wrapper;
// Uses C++17 metafunction for brevity
template <class T, class... Ts>
struct all_same : std::conjunction<std::is_same<T, Ts>...>
{};
template <class T, class Enable = void>
struct test;
template <template <class...> class Wrapper, class... Ts>
struct test<Wrapper<Ts...>, std::enable_if_t<all_same<Ts...>::value>>
{};
int main()
{
test<wrapper<int, int>> t;
//test<wrapper<int, double>> t1; // won't compile, as we haven't defined the primary template
}