Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ – Why does aggregate initialization not work with template struct

This code works, without having to specify a constructor:

struct Foo
{
    int a;
    int b;
};

//...

    int a1, b1;

    Foo foo = {a1, b1};

If I make Foo a template, it doesn’t work.

template<typename T1, typename T2>
struct Foo
{
    T1 a;
    T2 b;
};

//...

    int a1, b1;

    Foo foo = {a1, b1};

It says deduction failed / 2 arguments were provided while 1 expected. If I add a constructor like Foo(T1, T2){} then it works. I thought, that sort of construction just works by default for structs. What am I getting wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

EDIT:
I’m using Clang, which seems not to support it. Both MSVC and GCC compile it with c++20 compiler flag.

>Solution :

Since C++20 aggregates have implicitly-generated deduction guide so class template argument deduction works for aggregates too.

int a1, b1;
Foo foo = {a1, b1}; // works since C++20; T1 and T2 are deduced as int

Before C++20, you need to add user-defined deduction guide, e.g.

template<typename T1, typename T2>
struct Foo
{
    T1 a;
    T2 b;
};

template<class T1, class T2> Foo(T1 a, T2 b) -> Foo<T1, T2>;

Clang has not supported class template argument deduction for aggregates yet.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading