非类右值始终具有 cv 未限定的类型
§3.10 第 9 节说“非类右值总是具有 cv 不合格的类型”。这让我想知道......
int foo()
{
return 5;
}
const int bar()
{
return 5;
}
void pass_int(int&& i)
{
std::cout << "rvalue\n";
}
void pass_int(const int&& i)
{
std::cout << "const rvalue\n";
}
int main()
{
pass_int(foo()); // prints "rvalue"
pass_int(bar()); // prints "const rvalue"
}
根据标准,不存在非类类型的 const 右值,但 bar()
更喜欢绑定到 const int&&
。这是编译器错误吗?
编辑:显然, this
也是一个 const 右值:)
编辑:这个问题似乎在 g++ 4.5.0 中得到了修复,两行现在都打印“rvalue”。
§3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder...
int foo()
{
return 5;
}
const int bar()
{
return 5;
}
void pass_int(int&& i)
{
std::cout << "rvalue\n";
}
void pass_int(const int&& i)
{
std::cout << "const rvalue\n";
}
int main()
{
pass_int(foo()); // prints "rvalue"
pass_int(bar()); // prints "const rvalue"
}
According to the standard, there is no such thing as a const rvalue for non-class types, yet bar()
prefers to bind to const int&&
. Is this a compiler bug?
EDIT: Apparently, this
is also a const rvalue :)
EDIT: This issue seems to be fixed in g++ 4.5.0, both lines print "rvalue" now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
委员会似乎已经意识到标准的这一部分存在问题。 CWG 问题 690 讨论了一个有些类似的问题与标准的完全相同的部分(在 2009 年 9 月的“附加说明”中)。我猜想很快就会为该标准的这一部分起草新的语言。
编辑:我刚刚提交了一篇关于 comp.std.c++ 的帖子,指出了问题并建议标准相关部分的新措辞。不幸的是,作为一个经过审核的新闻组,当这个问题通过那里的批准队列时,几乎每个人都可能已经忘记了这个问题。
The committee already seems to be aware that there's a problem in this part of the standard. CWG issue 690 talks about a somewhat similar problem with exactly the same part of the standard (in the "additional note" from September, 2009). I'd guess new language will be drafted for that part of the standard soon.
Edit: I've just submitted a post on comp.std.c++, noting the problem and suggesting new wording for the relevant piece of the standard. Unfortunately, being a moderated newsgroup, nearly everybody will probably have forgotten this question by the time it makes it through the approval queue there.
好点。我想有两件事需要注意:1)正如您指出的非类右值 thingy 和 2)重载解析如何工作:
我没有在标准中看到任何内容告诉我在重载解析期间非类右值会受到特殊处理。
你的问题在我的标准草案(N-4411)中有所涉及:
然而,真正发挥作用的是对引用绑定、隐式转换序列、引用和一般重载解析的并行读取:
13.3 .3.1.4 引用绑定
和
13.3.3.2 对隐式转换序列进行排名
Good point. I guess there are two things to look at: 1) as you pointed out the non-class rvalue thingsy and 2) how overload resolution works:
I haven't seen anything in the standard that tells me non-class rvalues are treated specially during overload resolution.
Your question is covered in the draft of the standard I have though (N-4411) somewhat:
What does come into play is however a parallel reading of reference binding, implicit conversion sequences, references, and overload resolution in general:
13.3.3.1.4 Reference binding
and
13.3.3.2 Ranking implicit conversion sequences