简体   繁体   English

在C ++模板中使用声明?

[英]Use of declarations in C++ templates?

In a template-declaration, explicit specialization, or explicit instantiation the init-declarator-list in the declaration shall contain at most one declarator. 在模板声明,显式专业化或显式实例化中,声明中的init-declarator-list最多应包含一个声明器。 When such a declaration is used to declare a class template,no declarator is permitted. 当使用此类声明声明类模板时,不允许使用声明符。

Any one explain this ? 有人解释吗?

For me it is necessary that i need to check the compilers is following the ISO standard? 对我来说,我需要检查编译器是否遵循ISO标准? It is our project please help me? 这是我们的项目,请帮助我吗?

That means you cannot write 这意味着你不能写

template <class T> class A{} a, b;

or similarly 或类似

template <class T> A<T>::a=0, A<T>::b=1;

(imagine what would a , b be in the first case). (想象一下,在第一种情况下aba什么)。 For a more thorough explanation of declarators, see chapter 8 of the standard. 有关声明符的更详尽说明,请参见标准的第8章。

Back when C was king, it used to be common practice to write code like this: 早在C为王时,编写这样的代码曾经是一种常见的做法:

struct { int x; int y; } pt1, pt2;

This code creates an anonymous struct and declares two variables of that type. 此代码创建一个匿名结构,并声明该类型的两个变量。

The standardese you quote basically says that you can't do that for template types. 您引用的标准语基本上说您不能对模板类型执行此操作。 Thus, the following is ill-formed: 因此,以下是不正确的形式:

template <typename T> struct { T x; T y; } pt1, pt2;

The reason should be obvious--pt1 & pt2 don't have complete type--we don't know what T is when we declare them. 原因应该很明显-pt1和pt2没有完整的类型-我们在声明它们时不知道T是什么。

That style of declaration is quite uncommon in C++, however, so this is a somewhat inconsequential restriction. 但是,这种声明样式在C ++中并不常见,因此这是一个无关紧要的限制。

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

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