gpt4 book ai didi

c++ - 进程引用初始化时是否是clang中的错误

转载 作者:行者123 更新时间:2023-12-01 14:08:04 25 4
gpt4 key购买 nike

#include <iostream>
struct B;
struct A{
operator B&&() const;
};
struct B{
B(A const&){

}
B() {}
};
int main(){
A a;
B&& rf = a; //#1
}

B g;
A::operator B&&() const {
std::cout<<"execute\n";
return std::move(g);
}
考虑上面的代码,结果是 here . #1 处的引用绑定(bind)遵守这些规则:

Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be const), or the reference shall be an rvalue reference.

  • If the initializer expression
  • is an rvalue (but not a bit-field) or function lvalue and “cv1 T1” is reference-compatible with “cv2 T2”, or
  • has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be converted to an rvalue or function lvalue of type “cv3 T3”, where “cv1 T1” is reference-compatible with “cv3 T3” (see [over.match.ref]),


then the value of the initializer expression in the first case and the result of the conversion in the second case is called the converted initializer. If the converted initializer is a prvalue, its type T4 is adjusted to type “cv1 T4” ([conv.qual]) and the temporary materialization conversion is applied. In any case, the reference is bound to the resulting glvalue (or to an appropriate base class subobject).



  • Otherwise:
  • If T1 or T2 is a class type and T1 is not reference-related to T2, user-defined conversions are considered using the rules for copy-initialization of an object of type “cv1 T1” by user-defined conversion ([dcl.init], [over.match.copy], [over.match.conv]); the program is ill-formed if the corresponding non-reference copy-initialization would be ill-formed. The result of the call to the conversion function, as described for the non-reference copy-initialization, is then used to direct-initialize the reference. For this direct-initialization, user-defined conversions are not considered.


根据上述规则的结构, If 的第二个项目符号对于 B&& rf = a; 来说,分支就足够了,因此 operator B&&() const类(class) A是唯一的候选转换函数,也就是说,只要 if如果满足,则 otherwise的分支永远不会下。 GCC 的结果然而,证明这些规则所说的内容 Clang提示用于执行引用绑定(bind)的转换函数不明确( Clang 似乎将分别分支中的两个转换函数视为候选函数)。它是clang中的错误吗?
即使这样, case
#include <iostream>
struct B;
struct A{
operator B&&() const;
};
struct B{
B(A&){

}
B() {}
};
int main(){
A a;
B&& rf = a; //#1
}

B g;
A::operator B&&() const {
std::cout<<"execute\n";
return std::move(g);
}
GCC还是同意 operator B&&() const是执行引用绑定(bind)的唯一转换函数。

最佳答案

是的,您选择了正确的段落 [dcl.init.ref] §5.2.1.2 :

Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be const), or the reference shall be an rvalue reference.

  • If the initializer expression
  • is an rvalue [...]
  • has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be converted to an rvalue or function lvalue of type “cv3 T3”, where “cv1 T1” is reference-compatible with “cv3 T3” (see [over.match.ref]),


引用初始化的目的是首先尝试直接绑定(bind)(定义在 [dcl.init.ref] 节的最后一个规范句子中,简而言之:直接绑定(bind)发生在初始化器和引用是引用相关的或者如果存在是一个转换函数,其结果类型是与初始化引用相关的引用)。所以这是一个 Clang 错误,这当然不是标准中的开放问题。
(可能引起怀疑的站点开放问题(CWG2028)与某些“直接绑定(bind)”可能涉及临时实现的事实有关,因此这些情况不应该是直接引用绑定(bind),而是间接绑定(bind)到用户定义的结果转换。)

关于c++ - 进程引用初始化时是否是clang中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63083991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com