2014-03-14 62 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的值函数模板实例化不是将模板参数编码到结果类型中。

这对于类模板是不同的。

相关问题