c++c++17ctad

Class template argument deduction of member variables


In C++17 I am allowed to use Foo in the following example without empty template argument brackets thanks to class template argument deduction:

template<typename T = int>
struct Foo{};

int main(){
    Foo f;    // before C++17 you had to write "Foo<> f;"
}

Why am I not allowed to use the same syntax for class members?

template<typename T = int>
struct Foo{};

struct Foo2{
    Foo f{};  ///< error: invalid use of template-name 'Foo' without an argument list
};

int main(){
    Foo2 f2;
}

Solution

    1. Nobody, IIRC, proposed it.
    2. Presumably for the same reason we don't deduce anything from default member initializers: they are not always used - a constructor can override them by explicitly specify a different initializer.