[英]C++17 pointer to tempalte class with class template default argument
I am experimenting with C++17 class template default argument and was wondering if anyone could explain:我正在试验 C++17 类模板默认参数,想知道是否有人可以解释:
If I have the following:如果我有以下情况:
template<typename Policy = DefaultPolicy>
class MyClass { //class goes here };
And then try to use it as:然后尝试将其用作:
MyClass * class = new MyClass();
I get the error:我得到错误:
However both the following compile OK:但是以下两个编译都可以:
MyClass stackClass = MyClass();
auto * heapClass = new MyClass();
In particular I am very interested in how auto is working above.特别是我对 auto 在上面的工作方式非常感兴趣。 Thanks so much!
非常感谢!
Perhaps there is also a concept name that describes this that I can google for more info also.也许还有一个概念名称来描述这一点,我也可以在谷歌上搜索更多信息。
Working example: https://godbolt.org/z/EbEnxjcej工作示例: https ://godbolt.org/z/EbEnxjcej
Correct syntax forming a pointer to a template instance with default parameter would be:形成指向具有默认参数的模板实例的指针的正确语法是:
MyClass<> * heapClass = new MyClass();
auto smartClass = std::make_unique<MyClass<>>(); // for same reason
MyClass
formally isn't a type-id, it's a template name. MyClass
正式不是类型标识,而是模板名称。 That's why make_unique
would fail, because its parameter should be a typename
.这就是
make_unique
会失败的原因,因为它的参数应该是typename
。 Pointer declaration syntax would require same.指针声明语法需要相同。 What
auto
does is use of a full type-id - MyClass<DefaultPolicy>
. auto
所做的是使用完整的类型 ID - MyClass<DefaultPolicy>
。 The new expression is one of special cases allowed in C++17 along with MyClass stackClass
although for clarity new MyClass<>()
can be used as pre-17.新表达式是 C++17 中允许的特殊情况之一,与
MyClass stackClass
一起,尽管为了清楚起见new MyClass<>()
可以用作 17 之前的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.