简体   繁体   English

类模板参数推导成员变量

[英]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: 在C ++ 17中,由于类模板参数推导,我可以在以下示例中使用Foo而不使用空模板参数括号:

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. 没有人,IIRC,提出它。
  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. 据推测,出于同样的原因,我们不会从默认成员初始化器中推导出任何内容:它们并不总是被使用 - 构造函数可以通过显式指定不同的初始化程序来覆盖它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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