非类右值始终具有 cv 未限定的类型

发布于 2024-08-19 13:40:57 字数 602 浏览 8 评论 0原文

§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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

用心笑 2024-08-26 13:40:57

委员会似乎已经意识到标准的这一部分存在问题。 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.

别闹i 2024-08-26 13:40:57

好点。我想有两件事需要注意:1)正如您指出的非类右值 thingy 和 2)重载解析如何工作:

最佳选择标准
函数是参数的数量,
论点与论点的匹配程度如何
候选者的参数类型列表
函数,[...]

我没有在标准中看到任何内容告诉我在重载解析期间非类右值会受到特殊处理。

你的问题在我的标准草案(N-4411)中有所涉及:

然而,真正发挥作用的是对引用绑定、隐式转换序列、引用和一般重载解析的并行读取:

13.3 .3.1.4 引用绑定

2 当参数为引用类型时
不直接绑定到参数
表达式,转换序列
是将参数表达式转换为底层所需的表达式
参考文献的类型
至 13.3.3.1。

13.3.3.2 对隐式转换序列进行排名

3 两个隐式转换序列
相同的形式是无法区分的
转换序列,除非其中之一
以下规则适用:

— 标准转换序列 S1 是比标准转换序列更好的转换序列
标准
转换序列S2 if

——S1 和 S2 是引用绑定 (8.5.3),并且都不引用
a 的隐式对象参数
非静态的
声明的成员函数没有引用限定符,并且 S1 绑定一个
左值参考
到左值并且 S2 绑定右值引用或 S1 绑定右值
对右值和 S2 的引用
绑定左值引用。

[示例:

int i;
int f();
int g(const int&);
int g(const int&&);
int j = g(i); // calls g(const int&)
int k = g(f()); // calls g(const int&&)

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:

The selection criteria for the best
function are the number of arguments,
how well the arguments match the
parameter-type-list of the candidate
function, [...]

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

2 When a parameter of reference type
is not bound directly to an argument
expression, the conversion sequence
is the one required to convert the argument expression to the underlying
type of the reference according
to 13.3.3.1.

and

13.3.3.2 Ranking implicit conversion sequences

3 Two implicit conversion sequences of
the same form are indistinguishable
conversion sequences unless one of the
following rules applies:

— Standard conversion sequence S1 is a better conversion sequence than
standard
conversion sequence S2 if

— S1 and S2 are reference bindings (8.5.3) and neither refers to an
implicit object parameter of a
nonstatic
member function declared without a ref-qualifier, and either S1 binds an
lvalue reference
to an lvalue and S2 binds an rvalue reference or S1 binds an rvalue
reference to an rvalue and S2
binds an lvalue reference.

[ Example:

int i;
int f();
int g(const int&);
int g(const int&&);
int j = g(i); // calls g(const int&)
int k = g(f()); // calls g(const int&&)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文