简体   繁体   English

具有一个参数和默认值的类模板

[英]Class template with one parameter and default value

I must be missing something obvious here because this really came to me as a surprise. 我必须在这里遗漏一些明显的东西,因为这真的让我惊讶。

The following code gives me the error: error: missing template arguments before 'a' 以下代码给出了错误: error: missing template arguments before 'a'

template<int n=0>
class A {};

...
A a;
...

Is there a reason why a template with 1 parameter declared with a default value has to be instantiated specifying a value for it? 是否有必要实例化为具有默认值声明的1参数的模板指定其值?

Can someone quote the standard? 有人引用标准吗?

A is still a template class. A仍然是模板类。 But you can omit the template parameter though: 但您可以省略模板参数:

template<int n=0>
class A {};

int main() {
    A<> a;
  // ^^ 
    return 0;
}

See the live demo . 查看现场演示


I think 1 the relevant standard section is here 我认为1相关的标准部分就在这里

14.7 Template instantiation and specialization 14.7模板实例化和专业化

... ...
3 An explicit specialization may be declared for a function template, a class template, a member of a class template or a member template. 3可以为函数模板,类模板,类模板的成员或成员模板声明显式特化。 An explicit specialization declaration is introduced by template<>. 模板<>引入了显式特化声明。 In an explicit specialization declaration for a class template, a member of a class template or a class member template, the name of the class that is explicitly specialized shall be a simple-template-id. 在类模板的显式特化声明,类模板的成员或类成员模板中,显式专用的类的名称应为simple-template-id。 In the explicit specialization declaration for a function template or a member function template, the name of the function or member function explicitly specialized may be a template-id. 在函数模板或成员函数模板的显式特化声明中,显式专用的函数或成员函数的名称可以是template-id。
[Example: [例:

  template<class T = int> struct A { static int x; }; template<class U> void g(U) { } template<> struct A<double> { }; // specialize for T == double template<> struct A<> { }; // specialize for T == int template<> void g(char) { } // specialize for U == char // U is deduced from the parameter type template<> void g<int>(int) { } // specialize for U == int template<> int A<char>::x = 0; // specialize for T == char template<class T = int> struct B { static int x; }; template<> int B<>::x = 1; // specialize for T == int 

— end example ] - 结束例子]


1) Sorry, I can't find a better standard cite / example. 1) 抱歉,我找不到更好的标准引用/示例。 It doesn't cover exactly the OP's case, but demonstrates at least the same, that templates still need the <> angle brackets to be identified as templates (classes or functions). 它并未完全涵盖OP的情况,但至少表明相同,模板仍然需要将<>尖括号标识为模板(类或函数)。

possible workaround if it's important to disguise the nature of your object: 如果重要的是伪装对象的性质,可能的解决方法:

// the template class
template<int n=0>
class ReallyA {};

// an alias for the common case    
using A = ReallyA<0>;

A a;

This is how the standard library defines std::string , for example. 例如,这是标准库定义std::string

using string = basic_string<char>;

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

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