不能有汽车朋友
struct X
{
private:
int value_;
public:
X():value_(int())
{}
X(int value):value_(value)
{}
friend
//int operator+(X lhs, X rhs);//THIS WILL WORK
//BUT THE ONE BELOW WON'T
auto operator+(const X& lhs, const X& rhs)->decltype(lhs.value_ + rhs.value_);
};
auto operator+(const X& lhs, const X& rhs)->decltype(lhs.value_ + rhs.value_)
{//If I change return type to "not auto" there is no problem with friendship
return lhs.value_ + rhs.value_;
}
int main(int argc, char* argv[])
{
X a(5);
X b(6);
cout << a + b;
return 0;
}
当返回类型为auto时,无法声明与operator+的友谊。有什么解决办法吗?
struct X
{
private:
int value_;
public:
X():value_(int())
{}
X(int value):value_(value)
{}
friend
//int operator+(X lhs, X rhs);//THIS WILL WORK
//BUT THE ONE BELOW WON'T
auto operator+(const X& lhs, const X& rhs)->decltype(lhs.value_ + rhs.value_);
};
auto operator+(const X& lhs, const X& rhs)->decltype(lhs.value_ + rhs.value_)
{//If I change return type to "not auto" there is no problem with friendship
return lhs.value_ + rhs.value_;
}
int main(int argc, char* argv[])
{
X a(5);
X b(6);
cout << a + b;
return 0;
}
Cannot declare a friendship with operator+ in scenario when return type is auto. Any solution for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这适用于 g++ 4.5.1:
This works with g++ 4.5.1:
它看起来像是一个编译器错误,而不是任何故意的(哪个编译器?考虑提交一个错误)。但是,您始终可以执行 带有函数的
auto
规范(当然避免这是新语法的动机,但如果它不起作用......),即:两边都有相同的 X ,因此您可以在 X 中创建 typedef,这样您就不必在运算符的声明和定义中键入这种令人讨厌的内容。
(编辑)对于两侧没有相同 X 的情况,您仍然可以通过使用 decltype 准备模板(特征)并使用它来声明运算符来分解声明,这样就不会太可怕了。像这样的东西:
It looks like a compiler bug rather than anything intentional (which compiler? consider filing a bug). However, you can always do the ugly alternative described in the specification for
auto
with functions (of course avoiding that is motivation for the new syntax, but if it does not work...), namely:You have the same X on both sides, so you can make a typedef in X so you don't have to type out this obnoxiousness both in declaration and definition of the operator.
(Edit) For cases where you don't have the same X on both sides, you can still factor the declaration so that it's not too horrible by preparing a template (trait) with the decltype and using it to declare the operator. Something like:
在我看来,在你的情况下,使用 auto 没有多大意义,你最好说操作符返回 int。
这里的问题是,为了能够编写 rhs.value_ rhs 必须是完整类型的实例。在类结束 } 后,类型 X 被认为是完整的。所以编译器有理由抱怨。
EDIT3:
因为在我看来,我的上述解释并不完整,可能会导致混乱。
编辑:
有关 VS2010 与 decltype 相关的讨论,解释了为什么它在 VS2010 中是这样的,还解释了为什么它是标准兼容的,你可以查看 此处:
我的第一个选择是删除 auto。但是,假设您提供了一个更简单的示例来进行说明,并且在您的实际情况下 auto 实际上是有意义的,您可以尝试:
EDIT2:
为了完整性(并说明这不是 auto 和朋友的问题,而是尝试使用不完整的问题)类型考虑以下代码(这也实现了你想要的,但以一种扭曲的不推荐的方式:-)):
代码在VS2010和gcc-4.5.1中编译正常
In my opinion, in your situation, using auto does not make all that much sense, you would be probably better off saying the operator returns int.
The issue here is that in order to be able to write rhs.value_ rhs must be an instance of a complete type. The type X is considered complete after the closing } of the class. So the compiler is rightfully complaining.
EDIT3:
Since it seems to me my above explanation was not complete and could lead to confusion.
EDIT:
For a discussion on VS2010 relating to decltype that explains why it is the way it is in VS2010 and also explains why it is standard compilant how it is in VS2010 you can look here:
My first option would be to drop auto. However, assuming you have provided a simpler example for illustration, and in your real case auto would actually make sense you could try:
EDIT2:
For completness (and to illustrate this is not an issue with auto and friend but rather attempting to use an incomplete type consider the following code (that also achieves what you want but in a twisted not recommendable way :-) ):
The code compiles ok in VS2010 and gcc-4.5.1