我想使用函式指標作為非型別模板引數。我可以使用typedef或使用函式指標型別的預定義別名來完成using。沒有預定義型別別名的模板定義是否有任何語法?
bool func(int a, int b) { return a==b; }
//using FUNC_PTR_T = bool(*)(int,int); // OK
typedef bool(*FUNC_PTR_T)(int,int); // also OK
template < FUNC_PTR_T ptr >
void Check()
{
std::cout << ptr( 1,1 ) << std::endl;
}
int main()
{
Check<func>();
}
我想在一個步驟中寫入,例如:
// this syntax did not compile...
template < bool(*)(int,int) ptr >
void Check()
{
ptr(1,1);
}
有人可以為我指出一個有效的語法嗎?
uj5u.com熱心網友回復:
您將要宣告一個非型別模板引數。你可以寫
template < bool( *ptr )( int, int ) >
void Check()
{
ptr( 1, 1 );
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361047.html
