简体   繁体   中英

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 standard? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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