2014-03-14 63 views
0

是否可以定義「模板函數指針」的類型?如是否可以定義「模板函數指針」的類型?

void (*tfp<99>)(); // can't compile 

就像模板類可以這樣做:

Someclass<99>(); 

爲了進一步說明我的問題,我有以下代碼:

template <int N> 
class Foo {}; 

template <int N> 
void foo() {} 

template <int N, template <int> class OP> 
void test_foo(OP<N> op) { std::cout << N << std::endl; } 

我可以叫test_foo(Foo<99>()),但可撥打test_foo()與論點foo<99>

test_foo(Foo<99>());     //OK 
test_foo(foo<99>);      //error: candidate template ignored: could not match '<N>' against 'void (*)()' 
test_foo<void (*)()>(foo<99>);   //error: candidate template ignored: invalid explicitly-specified argument for template parameter 'N' 
test_foo<void (*<99>)()>(foo<99>);  //error: expected expression 
test_foo<void (*<99>)()<99> >(foo<99>); //error: expected expression 

有沒有辦法可以從foo<99>test_foo()得到模板參數N就像test_foo(Foo<99>())呢?

回答

1

你可以在C++ 11模板別名:

template <int V> using fptr = void (*tfp<V>)(); 

你不能這樣做「特質」這些FPTR值(或推斷從&foo<N>函數參數模板參數),因爲a的值函數模板實例化不是將模板參數編碼到結果類型中。

這對於類模板是不同的。

相關問題