34

Consider the following code snippet:

#include <iostream>

struct A {
  A() {}
  A(const A&) {}
};

struct B {
  B(const A&) {}
};

void f(const A&) { std::cout << "A" << std::endl; }
void f(const B&) { std::cout << "B" << std::endl; }

int main() {
  A a;
  f(   {a}   ); // A
  f(  {{a}}  ); // ambiguous
  f( {{{a}}} ); // B
  f({{{{a}}}}); // no matching function
}

Why does each call fabricate the corresponding output? How does the number of braces affect uniform initialization? And how does brace elision affect all this?

9
  • Completely attracted. But I think you' d better post your complier message. :)
    – con ko
    Commented Feb 3, 2019 at 15:50
  • 1
    @Constructor the comments give the error messages when compiling with (at least) g++, else what is written during the execution
    – bruno
    Commented Feb 3, 2019 at 15:51
  • 3
    may want to add the tag language-lawyer
    – Eljay
    Commented Feb 3, 2019 at 16:03
  • 3
    Nit: semicolons after member function definitions are pointless and let the uninitiated imagine things like all closing braces should get one. Commented Feb 3, 2019 at 17:39
  • 2
    @Rakete1111: There are more uses for null statements than null declarations (that are not also statements). Commented Feb 3, 2019 at 19:32

1 Answer 1

18

Overload resolution is fun like this.

  1. {a} has exact match rank for initializing (a temporary for) the const A& parameter, which outcompetes the user-defined conversion B(const A&) as a realization of {a}. This rule was added in C++14 to resolve ambiguities in list-initialization (along with adjustments for aggregates).

    Note that the notional temporary is never created: after overload resolution picks f(const A&), the reference is simply initialized to refer to a, and this interpretation can apply even for non-copyable types.

  2. It would be permissible to initialize a const A& parameter (as above) to the constructor for either A or B, so the call is ambiguous.
  3. Calling a copy constructor (here, A(const A&)) repeatedly is prohibited as multiple user-defined conversions—rather than allowing one such conversion per level of overload resolution. So the outermost braces must initialize a B from the A initialized from {{a}} as (permitted) in the second case. (The middle layer of braces could initialize a B, but copying it with the outer layer would be prohibited and there’s nothing else to try to initialize.)
  4. Every interpretation involves such a disallowed extra conversion.

No brace elision is involved—we don’t know the outermost target type to allow it.

15
  • Just to make sure that I understand it correctly: f({a}) can be realized by f(A{a}) or f(B{a}). Since the first is a perfect match, it is a better match than the second, and the call is unambiguous. Now we consider the second case. f({{a}}) can be realized by f(A{A{a}}), f(B{A{a}}), and f(B{B{a}}). The first is prohibited and the second and the third are equally well matches, thus the call is ambiguous. Now consider the third case. f({{{a}}}) can be realized by f(A{A{A{a}}}), f(B{A{A{a}}}), f(B{B{A{a}}}), and f(B{B{B{a}}}). Only the third is not prohibited. Commented Feb 3, 2019 at 19:59
  • I think I haven't understood it yet. If the problem was that f(B{A{a}}) and f(B{B{a}}) are equally well matches, the call would still be ambiguous when we remove f(const A&). However, the compiler says the opposite. Commented Feb 3, 2019 at 20:25
  • @user1494080: The ambiguity is between f(A{A{a}}) (only the outer of those two is user-defined) and f(B{A{a}}). f(B{B{a}}) is invalid because the inner conversion is user-defined and so the outer one must not be. Note the asymmetry: {a} -> A is exact-match, but the resulting {B{a}} -> B is user-defined because overload resolution had to occur to identify the {a} -> B conversion. Commented Feb 3, 2019 at 22:33
  • Doesn't f(A{A{a}}) call two times one and the same copy-constructor A(const A&)? How can one be user-defined and the other not? Commented Feb 4, 2019 at 0:11
  • 1
    @DavisHerring Sorry, I’m still struggling. f(B{B{a}}) is prohibited, because the inner conversion is user-defined (I agree), and the outer conversion is also user-defined (that's how I understand you). However, in the third case f(B{B{A{a}}}) is not prohibited, because the inner conversion is an exact match, the middle conversion is user-defined, and the outer one is again a standard conversion? Why is the first f(B{B{...}}) user-defined and the second f(B{B{...}}) not? Commented Feb 4, 2019 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.