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).
不幸的是,我不是c ++的期望,这似乎是语言的隐藏角落(至少对我而言)。
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'
如果我可以从指向函数的指针到它的类型 - 例如:std :: tr1 :: result_of <&fn> :: type //错误1错误C2923:'std :: tr1 :: result_of':'fn1 '不是参数'_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.
C ++ 0x具有decltype(&fn1),但距今已有数年。
Any way of doing this in C++03 (+ tr1)?
在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).)
限制: - 我不想传递函子,f1和f2必须保留具有返回值的全局函数(不能将其移动到参数)。)
1 个解决方案
#1
6
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:
这在C ++ 03中是不可能的。如果要将函数指针作为非类型参数传递,则编译器必须知道参数的类型。所以你必须提供缺失的部分(在这种情况下,返回类型)。您可以在运行时为代理提供函数指针作为值,并为其提供其类型作为唯一参数。然后你可以为你编写一个生成函数来完成这项工作:
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:
遗憾的是,在当前的C ++中,为了分配自动变量,你仍然必须给它类型:
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:
你不能使用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);
}
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.
如果doSomething是模板化的或其他多态的,它不需要你知道函数的确切类型。
#1
6
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:
这在C ++ 03中是不可能的。如果要将函数指针作为非类型参数传递,则编译器必须知道参数的类型。所以你必须提供缺失的部分(在这种情况下,返回类型)。您可以在运行时为代理提供函数指针作为值,并为其提供其类型作为唯一参数。然后你可以为你编写一个生成函数来完成这项工作:
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:
遗憾的是,在当前的C ++中,为了分配自动变量,你仍然必须给它类型:
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:
你不能使用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);
}
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.
如果doSomething是模板化的或其他多态的,它不需要你知道函数的确切类型。