A reference to const T initialized with a type value other than T

For the following code:

struct A {
    explicit A(int) {}
};

const A& a(1);    // error on g++/clang++/vc++
const A& b = 1;   // error on g++/clang++/vc++
const A& c{1};    // ok on g++/clang++, error on vc++
const A& d = {1}; // error on g++/clang++/vc++

Which of (1) initialization is legal?

If we ignore vC ++ first, it seems that the difference between direct-init and copy-init does not behave sequentially here. If the third one is well-formed because it is direct-init, why can't the first one, which is also direct init, compile? What is the logic behind this? Or is it just a bug for g ++ / clang ++ and does vC ++ handle it correctly?

+4
source share
2 answers

If you use copied-init lists, and the type of initialization destination is a link:

[dcl.init.list] / 3 (from n3690)

  • , E, T , , E, ; (. ) T, .

  • , T , praleue , T, direct-list-initialized, , . [: , , , - lvalue . - ]

  • , , .

const A& c{1}; const A& d = {1}; . direct-list- a const A, -list- a const A. -, explicit, , . [Over.match.list]/1.


init-, , . [dcl.init.ref]/5 :

  • "cv1 T1" - (8.5). .

- explicit ctor, . [over.match.copy]/1 ( ).


:

const A& c{1};

. , , / ctor explicit.

+5

. .

.

A, . , .

0

Source: https://habr.com/ru/post/1529664/


All Articles