Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class template argument deduction of member variables

Tags:

c++

c++17

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;
}
like image 415
Becon Avatar asked Dec 05 '18 17:12

Becon


1 Answers

  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.
like image 185
T.C. Avatar answered Nov 15 '22 04:11

T.C.