简体   繁体   中英

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;
}
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM