X
xmllmx
Dear all,
C++98 14.3.1 explicitly says as follows:
"A local type, a type with no linkage, an unnamed type or a type
compunded from any of these types shall not be used as a template-
argument for a template type-parameter."
And the Standard also immediately gives an example:
/*-- Source Code Begin --*/
template <class T> class X {};
void f()
{
struct S {};
X<S> x3; // error: local type used as template-argument
}
/*-- Source Code End --*/
Nevertheless, the source code should can be correctly compiled by
Visual C++. Moreover, C++0x 14.4.1 explicitly says differently as
follows:
"A template-argument for a template-parameter which is a type shall be
a type-id."
C++0x 14.4.1 also immediately gives some examples as follows:
/*-- Source Code Begin --*/
template <class T> class X {};
template <class T> void f(T t) {}
struct {} unnamed_obj;
void f()
{
struct A {};
enum { e1 };
typedef struct { } B;
B b;
X<A> x1; // OK
X<A*> x2; // OK
X<B> x3; // OK
f(e1); // OK
f(unnamed_obj); // OK
f(b); // OK
}
/*-- Source Code End --*/
Obviously, both of the most popular C++ compiler and C++0x relax the
restrictions put by C++98.
In my view, the new rules without restrictions are intuitive, easy to
use for programmers and easy to implement for compilers. What makes me
most puzzled is the hidden reason for the authors of C++98 to put such
counterintuitive restrictions on compilers and on us innocent
programmers? What's the WHY behind the C++98 restrictions?
To my experience during learning C++, C++ always trys not to put any
restrictions on programmers as much as possible except and ONLY except
the following two cases:
Case 1) One language feature/usage is dangerous and/or easy to misuse
so that some specific restrictions are necessary. e.g. restrictions on
const member functions.
case 2) One language feature/usage can not be implementd by the C++
compilers. e.g. restrictions on static_cast(staic_cast a pointer to a
virtual base to another pointer to its derived class which have at
least two immediate base classes. i.e the notorious diamond
inheritence.)
As the supreme authority in C++ world, I always regard C++98 as a
bible-like masterpiece. I don't believe the authors of C++98 had not
deeply thought the above-mentioned issue over and over before they
decided to put such restrictions into the standard. However, I indeed
could not get a convincing explanation only by myself. So I eagerly
hope someone can give me that. Any help will be highly appreciated,
many thanks in advance!
C++98 14.3.1 explicitly says as follows:
"A local type, a type with no linkage, an unnamed type or a type
compunded from any of these types shall not be used as a template-
argument for a template type-parameter."
And the Standard also immediately gives an example:
/*-- Source Code Begin --*/
template <class T> class X {};
void f()
{
struct S {};
X<S> x3; // error: local type used as template-argument
}
/*-- Source Code End --*/
Nevertheless, the source code should can be correctly compiled by
Visual C++. Moreover, C++0x 14.4.1 explicitly says differently as
follows:
"A template-argument for a template-parameter which is a type shall be
a type-id."
C++0x 14.4.1 also immediately gives some examples as follows:
/*-- Source Code Begin --*/
template <class T> class X {};
template <class T> void f(T t) {}
struct {} unnamed_obj;
void f()
{
struct A {};
enum { e1 };
typedef struct { } B;
B b;
X<A> x1; // OK
X<A*> x2; // OK
X<B> x3; // OK
f(e1); // OK
f(unnamed_obj); // OK
f(b); // OK
}
/*-- Source Code End --*/
Obviously, both of the most popular C++ compiler and C++0x relax the
restrictions put by C++98.
In my view, the new rules without restrictions are intuitive, easy to
use for programmers and easy to implement for compilers. What makes me
most puzzled is the hidden reason for the authors of C++98 to put such
counterintuitive restrictions on compilers and on us innocent
programmers? What's the WHY behind the C++98 restrictions?
To my experience during learning C++, C++ always trys not to put any
restrictions on programmers as much as possible except and ONLY except
the following two cases:
Case 1) One language feature/usage is dangerous and/or easy to misuse
so that some specific restrictions are necessary. e.g. restrictions on
const member functions.
case 2) One language feature/usage can not be implementd by the C++
compilers. e.g. restrictions on static_cast(staic_cast a pointer to a
virtual base to another pointer to its derived class which have at
least two immediate base classes. i.e the notorious diamond
inheritence.)
As the supreme authority in C++ world, I always regard C++98 as a
bible-like masterpiece. I don't believe the authors of C++98 had not
deeply thought the above-mentioned issue over and over before they
decided to put such restrictions into the standard. However, I indeed
could not get a convincing explanation only by myself. So I eagerly
hope someone can give me that. Any help will be highly appreciated,
many thanks in advance!