What is the supposed behavior of copy-list-initialization in the case of an initializer with a conversion operator?

前端 未结 1 817
灰色年华
灰色年华 2020-12-16 03:59
class AAA {
public:
    AAA() {}
    AAA(const AAA&) {}
};

class BBB {
public:
    BBB() {}
    operator AAA() { AAA a; return a; }
};

int main() {
    BBB b;
         


        
相关标签:
1条回答
  • 2020-12-16 04:31

    For the first, you are hitting http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1673 (see the last testcase): If list initialization passes only a single element to a copy/move constructor of some class X, user defined conversions are not allowed on that single element to convert it to the X parameter. Also see http://llvm.org/bugs/show_bug.cgi?id=12117 which made Clang implement this rule

    For the second: You are hitting http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1467 (but instead of using an object of the same type, you use an object of an unrelated type). Your aggregate simply doesn't provide a data member of type BBB.

    For the third: Neither of the above two situations apply, so the list initialization works and calls the CCC constructor of AAA. The = b initialization fails because it is only allowed to try converting the b to an AAA in a single user defined conversion sequence. But here you would need to first convert to CCC and then to AAA again. For list initialization, this restriction of doing only one user defined conversion does not exist.

    0 讨论(0)
提交回复
热议问题