decltype中的成员函数调用
以下代码:
struct A
{
int f(int);
auto g(int x) -> decltype(f(x));
};
无法编译并出现错误:
error: cannot call member function 'int B::f(int)' without object
如果我将其更改为:
struct A
{
int f(int);
auto g(int x) -> decltype(this->f(x));
};
我收到另一个错误:
error: invalid use of 'this' at top level
其中任何一个有什么问题?我正在使用海湾合作委员会4.6
The following code:
struct A
{
int f(int);
auto g(int x) -> decltype(f(x));
};
Fails to compile with the error:
error: cannot call member function 'int B::f(int)' without object
If I change it to:
struct A
{
int f(int);
auto g(int x) -> decltype(this->f(x));
};
I get another error:
error: invalid use of 'this' at top level
What is wrong with either of these? I am using gcc 4.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以下是神奇的词语:
编辑 我从 Mikael Persson 的回答中看到,这就是 boost 中的实现方式。
Here are the magic words:
Edit I see from Mikael Persson's answer that this is how it's done in boost.
目前,您只能访问“this”和函数体内的类成员,但这可能很快就会改变:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1207
Currently you can only access 'this' and members of the class inside the function body, but this is likely to be changed soon:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1207
result_of 和 decltype 组合可以给出成员函数的返回类型
result_of and decltype in combination can give the the return type for a member function
Comeau 不喜欢将
auto
作为顶级返回类型,但是以下编译成功:基本上,您必须声明至少一个附加构造来获得您想要的类型。并直接使用
decltype
。编辑:顺便说一句,这也适用于深入研究成员函数的返回类型:
当然,它没有 auto f()-> 那样的便利性。 ...,但至少它可以编译。
Comeau doesn't like
auto
as a top level return type, but the following compiles successfully:Basically, you have to declare at least one additional construct that gets you the type you want. And use
decltype
directly.EDIT: Incidentally, this works fine for diving into the return type of a member function as well:
Granted, it doesn't have the same convenience of
auto f()-> ...
, but at least it compiles.经过一些测试,
decltype(declval().f(x))
和decltype(((A*)0)->f(x))
会起作用。然而,似乎使用 boost::bind 可以工作(并且它是“幕后”版本):
当然,这并不漂亮。我想您可以研究一下 boost::bind 是如何做到的,也许会找到更好的解决方案。
编辑
正如MSN建议的,您还可以制作自己的函数模板来解决此问题:
After some tests, neither
decltype(declval<A>().f(x))
nordecltype(((A*)0)->f(x))
will work.However, it seems that using boost::bind will work (and it's "under-the-hood" version):
Of course, this is not pretty. I guess you can look into how boost::bind does it to maybe find a nicer solution.
EDIT
As MSN suggested, you can also make your own function template to resolve this:
在我看来,这不起作用,因为 decltype 位于方法之外,而 A 此时是一个不完整的类型(因此您甚至无法执行
A().f(x)
)。但你不应该真正需要它。在 A 的声明之外,这将按预期工作,在 A 中,您应该知道上面几行声明的函数的返回类型。或者你可以直接写:
这甚至适用于普通的 c++03。
Seems to me that does not work because the decltype is outside of the method and A is at that moment an incomplete type (so you can't even do
A().f(x)
).But you should not really need that. Outside of the declaration of A this will work as expected, in A you should know the return type of the function that you declared a few lines above. Or you could just write:
This even works with plain c++03.