获取没有对象的成员函数的返回类型 [英] Get return type of member function without an object

查看:78
本文介绍了获取没有对象的成员函数的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多无法修改的类。每个函数都有一个副本构造函数,至少一个其他构造函数和一个返回某些值的函数 foo()。我想制作一个可以从这些类的每一个派生的类模板,并具有与 foo()的返回类型相同的数据成员类型(对不起,如果我的某些术语有误)。

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).

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

In other words, I would like a class template

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

其中 footype T :: foo()

如果所有基类都具有默认构造函数,则可以

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

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

(在GCC中具有C ++ 0x功能),但是类没有任何特定的构造函数

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

GCC也不允许 decltype(this-> foo()),尽管显然有可能将其添加到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?

应该可以按照 decltype(foo()) decltype(T :: foo())但那些似乎不起作用:GCC给出了错误形式,无法在没有对象的情况下调用成员函数'int A :: foo()'

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.

当然,我可以有一个额外的模板参数 footype ,甚至还有一个类型为<$ c $的非类参数。 c> T ,但是有什么方法可以避免这种情况?

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?

推荐答案

您不需要那-请记住,由于decltype不评估其参数,因此您可以调用 nu llptr

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

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

这篇关于获取没有对象的成员函数的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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