#include <iostream>struct A{ A(int){ }};struct B{ B() = default; B(A){ } B(B const&){} B(B&&){}};int main(){ B b({0});}For the given codes, the candidate functions are: #1 B::B(A) #2 B::B(const B&) #3 B::B(B&&)According to the standard, for #1, the object of type A is copy-list-initialized by {0} as A a = {0}, A::A(int) is considered for the initialization, so only the standard conversion within #1. For #2, it's an initialization of a reference form braced-init-list which is the cause of [dcl.init.list] So it equates with const B& = {0}, in this initialization, the conversion function is B::B(A) and the argument is 0, so B tmp = {0} and 'B::B(A)' is considered that parameter is initialized by argument 0, as A parameter = 0.So there's a user-defined conversion within #2 and the situation of #3 is the same as that of #2 and accroding to the [over.ics.rank],The standard conversion is better than user-defined conversion, so #1 should be better than #2 and #3, but actually, g++ report the invocation is ambiguous, why? The error message is: main.cpp: In function ‘int main()’:main.cpp:12:10: error: call of overloaded ‘B(<brace-enclosed initializer list>)’ is ambiguous B b({0}); ^main.cpp:8:3: note: candidate: B::B(A) B(A){ ^main.cpp:6:8: note: candidate: constexpr B::B(const B&) struct B{ ^main.cpp:6:8: note: candidate: constexpr B::B(B&&) 解决方案 Answers are here,Because the argument is initializer list,so [over.ics.list] rules are performed when overload resolution.[over.ics.list]/6Hence,for these three candiate constructors,the parameter of them are all non-aggerate class type,so the implicit conversion sequence is used to convert the single element of the initializer list to corresponding parameter type of the constructor of parameter of constructor B and therefore these conversion sequences are all user-defined conversion sequence and the second standard conversion sequence are all identity conversions.So the rank of them are indistinguishable. 这篇关于复制列表初始化的隐式转换的等级是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 07:40