c ++推导“非类型指针到函数”类模板参数 [英] c++ deduction of "non type pointer to function" class template parameters

查看:229
本文介绍了c ++推导“非类型指针到函数”类模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑像下面这样的模板类:

  template< typename ReturnType,ReturnType Fn()> 
class Proxy
{
void run()
{
ReturnType ret = Fn();
// ... do something ...
}
};

//和函数
int fn1(){return 5; }
float fn2(){return 5; }

这可以通过使用实例化:

  Proxy< int,& fn1> p1; 

但是显式声明返回值类型看起来不必要。我想实现的是:

  someProxyInstantation<& fn1> p1; 
someProxyInstantation<& fn2> p2;

不幸的是,我没有c ++期望,这似乎是语言的一个隐藏的角落对我来说)。



如果我可以从指向函数的指针到它的类型 - 类似:
std :: tr1 :: result_of< ; fn> :: type //错误1错误C2923:'std :: tr1 :: result_of':'fn1'不是参数_Fty的有效模板类型参数

错误是有意义的,因为参数根本不是类型



C ++ 0x有decltype(& fn1) 。





$ b

在C ++ 03(+ tr1)中执行此操作的任何方式?

限制:
- 我不想传递函子,f1和f2必须保留具有返回值的全局函数(不能将其移动到参数)。

解决方案

这在C ++ 03中是不可能的。如果要将函数指针作为非类型参数传递,编译器必须知道参数的类型。所以你必须提供缺失的部分(在这种情况下,返回类型)。您可以在运行时将代理的函数指针作为一个值,并提供它的类型作为唯一的参数。然后你可以为你执行这个工作写一个生成器函数:

  template< typename T& 
Proxy< T> make_proxy(T t){return Proxy T(t); }

不幸的是,在当前C ++中,你仍然需要给它类型,自动变量:

 代理< int(*)()> p = make_proxy(& fn1); 

您不能使用 auto p = make_proxy(& fn1); 。注意,如果你想使用左边的函数类型,你必须改变生成器函数,不提供函数指针类型:

  template< typename T> 
Proxy< typename boost :: remove_pointer< T> :: type> make_proxy(T t){
return proxy< typename boost :: remove_pointer< T> :: type>(t);
}

现在你可以做

 代理< int()> p = make_proxy(& fn1); 

使用代理,您现在可以执行

  doSomething(make_proxy(& fn1)); 

如果doSomething是模板化的或其他多态的,它不需要你知道确切的类型函数。


Consider a template class like:

template<typename ReturnType, ReturnType Fn()>
class Proxy
{
    void run()
    {
    	ReturnType ret = Fn();
    	// ... do something ...
    }
};

// and a functions
int fn1() { return 5; }
float fn2() { return 5; }

This can be instantiated by using:

Proxy<int, &fn1> p1;

But explicitly declaring the return value type seems needless. What I am trying to achieve is something like:

 someProxyInstantation<&fn1> p1;
 someProxyInstantation<&fn2> p2;

Unfortunately, I'm no c++ expect and this seems like a hidden corner of the language (at least for me).

If I could just get from the pointer to the function to its type - something like: std::tr1::result_of<&fn>::type // Error 1 error C2923: 'std::tr1::result_of' : 'fn1' is not a valid template type argument for parameter '_Fty'

the error makes sense since the parameter is not a "type" at all

C++0x has the decltype(&fn1) but that is years away.

Any way of doing this in C++03 (+ tr1)?

Restrictions: - I don't want to pass the functor, f1 and f2 have to remain global functions that have a return value (can't move it to parameter).)

解决方案

This isn't possible in C++03. If you want to pass a function pointer as a non-type parameter, the compiler has to know the type of the parameter. So you have to provide the missing pieces (in this case, the return type). You can give the proxy the function pointer as a value at runtime, and provide it with the type of it as the only argument. Then you could write a generator function for you that does this job:

template<typename T>
Proxy<T> make_proxy(T t) { return Proxy<T>(t); }

Sadly, in current C++, you still have to give it the type in order to assign to a automatic variable:

Proxy<int(*)()> p = make_proxy(&fn1);

You can't use auto p = make_proxy(&fn1); yet. Note that if you want to use a function type on the left side, you have to change the generator function to provide not a function pointer type:

template<typename T>
Proxy<typename boost::remove_pointer<T>::type> make_proxy(T t) { 
    return Proxy<typename boost::remove_pointer<T>::type>(t); 
}

Now you can do

Proxy<int()> p = make_proxy(&fn1);

using the proxy, you can now just do

doSomething(make_proxy(&fn1));

And if doSomething is templated or otherwise polymorphic, it will not require you to know the exact type of the function.

这篇关于c ++推导“非类型指针到函数”类模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆