[英]Is it possible to define the type for “template function pointer”?
是否可以定義“模板函數指針”的類型? 如
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>())
,但是不能使用參數foo<99>
調用test_foo()
:
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
是否有辦法像test_foo(Foo<99>())
一樣從參數foo<99>
獲得test_foo()
的模板參數N
?
您可以在C ++ 11中使用模板別名:
template <int V> using fptr = void (*tfp<V>)();
您無法對這些fptr值執行“特征”(或從&foo<N>
函數參數推導模板參數),因為函數模板實例化的值不會將模板參數編碼為結果類型。
這與類模板不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.