Different implicit conversion in various std of C++

Viewed 80

How to keep track of implicit conversions between different stds of C++?

I have a following code:

struct Conv;

struct Test {
    Test() { puts("def"); }
    explicit Test(Test const& ref) { puts("copy"); }
    explicit Test(Test&& ref) { puts("move"); }
    Test(int) { puts("int"); }
};

struct Conv {
    operator Test() const  {
        puts("operator Test()");
        return 6;
    }
};

int main() {
   Conv y;
   Test t = {y}; // copy list init
}

If compiled using std=c++14 clang, error chosen constructor is explicit in copy-initialization But it compiles in std=c++17 clang and in std=c++20 clang

If I mark the conversion fn explicit,

explicit operator Test() const  {
            puts("operator Test()");
            return 6;
        }

in c++14 error: no matching constructor for the initialization of Test But in c++17 and 20 it compiles.

Is there any rule to how implicit conversions are ordered between converting constructor and conversion functions?

Edit: In clang c++17 and c++20 it prints

operator Test()
int

In c++14 error as stated above

0 Answers
Related