dimanche 3 février 2019

How does the number of braces affect uniform initialization?

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(    ); // ambiguous
  f( } ); // B
  f(}}); // 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?

Aucun commentaire:

Enregistrer un commentaire