开发者

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. 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 s开发者_StackOverflow中文版tandard? 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). For a more thorough explanation of declarators, see chapter 8 of the standard.


Back when C was king, it used to be common practice to write code like this:

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.

That style of declaration is quite uncommon in C++, however, so this is a somewhat inconsequential restriction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜