2013-07-24 23 views
0
template<int, int> 
struct T; 

template<> 
struct T<?, ?> {}; 

我想這個工作我能爲並非所有的非類型部分specizlize模板參數

typedef T<1, 0> t; 

,這導致編譯時錯誤

typedef T<1, 2> t; 

編輯,我的意思是我想第二個參數爲0.我不能使用C++ 11功能。

+0

當然,但失敗的條件是什麼?或者你想只是具體的數字? –

+0

[只接受某些類型的C++模板]的可能重複(http://stackoverflow.com/questions/874298/c-templates-that-accept-only-certain-types) –

+0

如果您希望第二個參數爲0 ,爲什麼不把它拋出? –

回答

1

您的問題不太清楚。你在找這個嗎?

template <int, int> 
struct T; 

template<int x> 
struct T<x, 0> 
{ 
    // Definition of the struct for the allowed case 
}; 
0

您可以使用static_assert來斷言模板參數。

template<int A, int B> 
struct T 
{ 
    static_assert(A > B, "Raised error because A is not bigger than B)"; 
}