2017-07-04 8 views
1

我試圖實現以下(用C++ 17功能):爲什麼沒有采用非類型的部分特化元組?

#include <type_traits> 

template<auto value_> 
using constant_t = std::integral_constant<decltype(value_), value_>; 

template<typename ... > class Tuple {}; 

template<auto ... values_> 
class Tuple<constant_t<values_> ... > {}; 

int main(void) 
{ 
    Tuple<int, int, char> types; 
    Tuple<1, 2, 3> values; 
} 

這給了我下面的錯誤以g ++ - 7.1.0

main.cpp: In function ‘int main()’: 
main.cpp:15:18: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class Tuple’ 
    Tuple<1, 2, 3> values; 
      ^
main.cpp:15:18: note: expected a type, got ‘1’ 
main.cpp:15:18: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class Tuple’ 
main.cpp:15:18: note: expected a type, got ‘2’ 
main.cpp:15:18: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class Tuple’ 
main.cpp:15:18: note: expected a type, got ‘3’ 

任何人都可以解釋爲什麼部分Tuple<1, 2, 3>專業化不啓動?

回答

9

1,2和3不是類型。您的專業化不會(也不能)將主模板更改爲接受值,所以您不能在以前預期類型的​​情況下神奇地傳遞值。

如果你想接受值的模板,別名模板可以做到這一點,而不是專業化:

template<auto... values_> 
using VTuple = Tuple<constant_t<values_>... >; 

但它是一個獨立的模板

+0

這就是我一直試圖避免(別名) –

+0

但謝謝你,你有解釋爲什麼主模板不能改變接受值? –

+4

@FletcherBlight - 專業化的本質。值不是一種更特殊的類型。它只是*有*類型。它有一個類型的事實不允許用它來代替它的類型。 – StoryTeller

相關問題