简体   繁体   English

返回指向成员函数的指针(没有typedef)

[英]Returning pointer-to-member-function (without typedefs)

Compiling on C++03, I've attempted to write up a test template function that returns a pointer-to-member-function of a member function that returns int, and takes two float arguments: 在C ++ 03上编译时,我试图编写一个测试模板函数,它返回一个返回int的成员函数的指向成员函数的函数,并获取两个float参数:

template<typename TemplateClass>
int (TemplateClass::*)(float,float) Testtest(TemplateClass &A)
{
    return &TemplateClass::Function;
}

But naturally, no matter what variations on the pointer-to-member-function syntax I use, the compiler complains of initialisation errors. 但很自然,无论我使用的指向成员函数的语法有什么变化,编译器都会抱怨初始化错误。 Typedef, although it works with known classes, for obvious reasons (naming conflicts), won't accept template class arguments for classes that I can't know ahead of time which are likely to use the same function. Typedef,虽然它适用于已知的类,但由于显而易见的原因(命名冲突),不会接受我不能提前知道可能使用相同函数的类的模板类参数。

What non-typedef way is there to get this function to compile and return a pointer-to-member-function? 有什么非typedef方法可以让这个函数编译并返回一个指向成员函数的指针?

To declare it without a type alias, without type deduction, and without a trailing return type: 要声明它没有类型别名,没有类型推导,并且没有尾随返回类型:

template<typename TemplateClass>
int (TemplateClass::* Testtest(TemplateClass &A))(float,float)

But of course this isn't what you would use in real code. 但当然这不是你在实际代码中使用的。 Instead you would use an alias template : 相反,您将使用别名模板

template<typename T>
using return_type = int (T::*)(float,float);

template<typename TemplateClass>
return_type<TemplateClass> Testtest(TemplateClass &A)

Or return type deduction in C++14: 或者在C ++ 14中返回类型推导:

template<typename TemplateClass>
auto Testtest(TemplateClass &A)

Or a trailing return type (in C++11): 或者是尾随返回类型(在C ++ 11中):

template<typename TemplateClass>
auto Testtest(TemplateClass &A) -> int (TemplateClass::*)(float,float)

You need this prototype: 你需要这个原型:

template<typename TemplateClass>
int (TemplateClass::*Testtest(TemplateClass &A)) (float,float) { }
  int (Class::*f())(float,float);

f is function taking no arguments returning pointer to member function of class Class takinf 2 floats and returning int. f是不带参数的函数返回指向类Class takinf 2浮点数并返回int的成员函数的指针。

And template version: 和模板版本:

         template <typename Type>
         int (Type::*f())(float,float);

I reject the premise of your question. 我拒绝你的问题的前提。 Use typedefs. 使用typedef。 Or, specifically, a type trait: 或者,具体来说,是一种类型特征:

template <class T, class F>
struct make_mem_fun {
    typedef F T::*type;
};

template<typename TemplateClass>
typename make_mem_fun<TemplateClass, int(float, float)>::type
Testtest(TemplateClass &A)
{
    return &TemplateClass::Function;
}

That is way easier to understand than the convoluted syntax of actually returning the type. 这比实际返回类型的复杂语法更容易理解。 With C++11, we can turn that into an alias to drop the typename ::type stuff. 使用C ++ 11,我们可以将其转换为别名以删除typename ::type内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM