获取没有对象的成员函数的返回类型

时间:2021-05-16 17:07:06

I have a number of classes that I cannot modify. Each has a copy constructor, at least one other constructor, and a function foo() that returns some value. I want to make a class template that can derive from each of these classes, and has a data member that is the same type as the return type of foo() (sorry if I've got some of the terminology wrong).

我有许多类我不能修改。每个函数都有一个复制构造函数、至少一个其他构造函数和一个函数foo(),该函数返回一些值。我想创建一个类模板,它可以从这些类中派生出来,并且具有与foo()的返回类型相同的数据成员(抱歉,如果我有一些术语错误的话)。

In other words, I would like a class template

换句话说,我想要一个类模板。

template<typename T> class C : public T
{
  footype fooresult;
};

where footype is the return type of T::foo().

foo类型是T的返回类型::foo()。

If the base classes all had, say, a default constructor, I could do

如果基类都有,比如说,一个默认构造函数,我可以。

decltype(T().foo()) fooresult;

(with the C++0x functionality in GCC) but the classes don't have any particular constructor in common, apart from the copy constructors.

(使用GCC中的c++ 0x功能)但是类除了复制构造函数之外没有任何特定的构造函数。

GCC also doesn't allow decltype(this->foo()), though apparently there is a possibility that this will be added to the C++0x standard - does anyone know how likely that is?

GCC也不允许decltype(这个->foo()),尽管很明显有可能会把它添加到c++ 0x标准中——有人知道这有多大可能吗?

I feel like it should be possible to do something along the lines of decltype(foo()) or decltype(T::foo()) but those don't seem to work: GCC gives an error of the form cannot call member function 'int A::foo()' without object.

我觉得应该可以按照decltype(foo()或decltype(T: foo())的方式来做一些事情,但是这些似乎都行不通:GCC给出的表单错误是不能在没有对象的情况下调用成员函数int A::foo()。

Of course, I could have an extra template parameter footype, or even a non-class parameter of type T, but is there any way of avoiding this?

当然,我可以有一个额外的模板参数footype,或者甚至是T类型的非类参数,但是有什么方法可以避免这种情况吗?

2 个解决方案

#1


56  

You don't need that- remember that since decltype doesn't evaluate its argument, you can just call on nullptr.

你不需要那个——记住,既然decltype没有评估它的参数,你可以只调用nullptr。

decltype(((T*)nullptr)->foo()) footype;

#2


30  

Another alternative is:

另一个选择是:

#include <utility>

template<typename T> class C : public T
{
   decltype(std::declval<T>().foo()) footype;
};

declval returns a T&&. Or if foo might be overloaded with rvalue-ref qualifiers, and you want to insure you get the lvalue overload of foo:

declval返回一个t。或者如果foo可能被rvalue-ref限定符重载,而您想要确保foo的lvalue重载:

   decltype(std::declval<T&>().foo()) footype;

In this example declval returns a T&.

在这个例子中,declval返回一个&。

Like the ((T*)nullptr)-> solution, std::declval places no requirements on the type T.

与(T*)nullptr)->解决方案类似,std::declval没有对类型T的要求。

#1


56  

You don't need that- remember that since decltype doesn't evaluate its argument, you can just call on nullptr.

你不需要那个——记住,既然decltype没有评估它的参数,你可以只调用nullptr。

decltype(((T*)nullptr)->foo()) footype;

#2


30  

Another alternative is:

另一个选择是:

#include <utility>

template<typename T> class C : public T
{
   decltype(std::declval<T>().foo()) footype;
};

declval returns a T&&. Or if foo might be overloaded with rvalue-ref qualifiers, and you want to insure you get the lvalue overload of foo:

declval返回一个t。或者如果foo可能被rvalue-ref限定符重载,而您想要确保foo的lvalue重载:

   decltype(std::declval<T&>().foo()) footype;

In this example declval returns a T&.

在这个例子中,declval返回一个&。

Like the ((T*)nullptr)-> solution, std::declval places no requirements on the type T.

与(T*)nullptr)->解决方案类似,std::declval没有对类型T的要求。