【发布时间】:2017-10-15 15:50:20
【问题描述】:
我想知道是否可以通过以下方式专门化模板:
template<typename... Ts>
class Typelist { };
template<template<typename> typename... TTs>
class Typelist<TTs<typename>> { }; //I don't know if that is the right syntax, at least the compiler doesn't complain
我希望它作为模板类型列表以及非模板类型列表工作:
template<typename T>
class AT {};
template<typename T>
class BT {};
template<typename T>
class CT {};
int main() {
using tl1 = Typelist<int, float, double>; //works fine
using tl2 = Typelist<AT, BT, CT>; //gives an error
}
编辑:
如果我将第二个 Typelist 声明为单独的类型,它可以工作...
template<template<typename> typename... TTs>
class Typelist2 { };
//...
using tl2 = Typelist2<AT, BT, CT>; //compiler doesn't complain and it works fine
我想知道是否可以只对这两种情况使用Typelist,这样Typelist2 就不必是单独的类型。
谁能帮帮我?
【问题讨论】:
-
AT,BT,CT是类的模板,您没有指定模板类型。如果没有以任何方式说明,编译器应该如何知道这些模板的基础类型?注意:模板是编译时构造。 -
@AlgirdasPreidžius 我相应地编辑了我的问题......
-
对于同一个参数,您不能让同一个模板有时将一个类型作为参数,而在其他时候将一个模板作为参数。每个模板参数要么是类型参数,要么是非类型参数,要么是模板参数;专业化无法改变这一点。
-
我认为
class Typelist<TTs<typename>>的语法不正确。
标签: c++ c++11 templates variadic-templates template-specialization