mercredi 19 février 2020

Why does template substitution fail on a constructor unless I add brackets?

I am trying to understand why substitution fails on the following snippet unless brackets are added:

template<typename T>
struct A {};

template<typename T>
struct B {
    B(A<T>);
};

template<typename T>
void example(A<T>, B<T>);

struct C {};

struct D {
    D(C);
};

void example2(C, D);

int main(int argc, char *argv[]) {
    example(A<int>{}, A<int>{}); // error
    example(A<int>{}, {A<int>{}}); // ok

    example2(C{}, C{}); // ok
    example2(C{}, {C{}}); // ok

    return 0;
}

See this example: https://godbolt.org/z/XPqHww

For example2 I am able to implicitly pass the C{} to the constructor of D without any error. For example I am not allowed to implicitly pass the A<int>{} until I add brackets.

What defines this behaviour?

Aucun commentaire:

Enregistrer un commentaire