[英]result_of of call to member function of template parameter
I need to get the result of a member function of a template parameter of a class. 我需要获取类的模板参数的成员函数的结果。 Unfortunately, I am bound to C++03 and cannot use decltype, but I can use tr1::result_of.
不幸的是,我绑定到C ++ 03,不能使用decltype,但是可以使用tr1 :: result_of。 I tried the following code, but that did not work with my compiler (gcc 4.3, I also cannot change this):
我尝试了以下代码,但是不适用于我的编译器(gcc 4.3,我也无法更改):
#include <tr1/functional>
struct ReturnType {};
struct O
{
ReturnType f();
};
template<typename T> struct A
{
typename std::tr1::result_of<(&T::f)(T*)>::type f();
};
void f()
{
A<O> a;
ReturnType x = a.f();
}
The above code reflects my understanding of result_of<Fn(ArgTypes ...)
: 上面的代码反映了我对
result_of<Fn(ArgTypes ...)
理解:
If Fn is a pointer to a non-static member function, and the first type in ArgTypes is the class the member belongs to (or a reference to it, or a reference to a derived type, or a pointer to it), and the remaining types in ArgTypes describe its arguments.
如果Fn是指向非静态成员函数的指针,并且ArgTypes中的第一个类型是该成员所属的类(或对其的引用,对派生类型的引用或对其的指针),则ArgTypes中的其余类型描述其参数。
I pass it a pointer to a member function and specify the first parameter type to be a pointer to the class. 我将其传递给成员函数的指针,并指定第一个参数类型为指向该类的指针。 However, the compiler prints the following error:
但是,编译器将显示以下错误:
result_of.cpp:12: error: `&' cannot appear in a constant-expression
result_of.cpp:12: error: a function call cannot appear in a constant-expression
result_of.cpp:12: error: template argument 1 is invalid
result_of.cpp:12: error: invalid use of ‘::’
result_of.cpp:12: error: expected ‘;’ before ‘f’
result_of.cpp: In function ‘void f()’:
result_of.cpp:18: error: ‘struct A<O>’ has no member named ‘f’
I cannot change the class O to eg add a result typedef, so I must be able to get the return type at compile time. 我不能将类O更改为例如添加结果typedef,因此我必须能够在编译时获取返回类型。
The std::tr1::result_of
requires a type parameter. std::tr1::result_of
需要一个类型参数。 You are passing it a non-type (a pointer to member). 您将其传递为非类型(指向成员的指针)。
This makes std::tr1::result_of
very limited in the absence of decltype
. std::tr1::result_of
没有decltype
这会使std::tr1::result_of
非常有限。 For instance you can use it in a wrapper function: 例如,您可以在包装函数中使用它:
template <typename Ct, typename Arg>
void some_wrapper(Ct fun, Arg arg)
{
typedef typename std::tr1::result_of<Ct(Arg)>::type ret;
ret result = fun(arg);
// ... do something with result
}
but you cannot use it like you are trying to. 但是您不能像尝试那样使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.