2015-06-11 110 views
6

为什么在可变参数模板包中没有允许特定类型?一种特定类型的可变参数模板参数

template< typename T > 
class Foo 
{ 
public: 
    template< typename... Values > 
    void bar(Values... values) 
    { 
    } 

    template< T... values >   <-- syntax error 
    void bar(T... values) 
    { 
    } 

    template< int... values >   <-- syntax error 
    void bar(int... values) 
    { 
    } 
}; 

什么在的理由让这个?
有没有这方面的建议?


注:替代将是

  • std::initializer_list<T>没有的类型变窄和{ } -brace语法
  • 一个(丑陋的)递归特性是seperately检查所有类型:see here

回答

6

它被允许,实际上,你只是用它错了。 T...int...是非类型参数包,它们的元素是值,因此不能将它们用作类型说明符(并且不能从函数调用中推导出它们)。

正确用法的例子:

template<int... Is> 
struct IntPack {}; 

IntPack<1,2,3> p; 

template< typename T > 
struct Foo 
{ 
    template< T... Ts> 
    void bar() 
    { 
    } 
}; 

int main() 
{ 
    Foo<int> f; 
    f.bar<1,2,3>(); 
} 

另一个例子是std::integer_sequence

相关问题