2015-06-11 108 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

相關問題