example sources in both Python and C++
Daniel Berlin
dberlin at redhat.com
Tue Oct 3 00:52:54 EDT 2000
wmcclain at salamander.com (Bill McClain) writes:
> > > * Template arguments cannot be local types. That means STL
> > > containers must use types declared at the outer scope, which
> > > is ugly.
> > Wait, what?
> > Explain what you mean, this is confusing.
> > If you mean you can only use types in the outermost, or global scope,
> > as template arguments, your compiler is severely broken.
>
> Try this on your compiler and tell me what happens:
>
> ***********
> #include <list>
>
> class Legal {
> int a;
> };
>
> int main() {
> class Illegal {
> int a;
> };
>
> list<Legal> ok; // compiles
> list<Illegal> nogood; // does not compile
> return 0;
> }
>
> // g++
> // temp.cpp:13: type `main()::Illegal' composed from a local type is not a valid template-argument
>
> // VMS
> // %CXX-E-LOCALTYPTMPLARG, a template argument may not reference a local type
> // at line number 13 in file CTS_DEVELOPER:[WMCCLAIN]TEMP.CPP;1
> ***********
>
> I'm too tired to turn on NT to try VC++.
>
> -Bill
Correct, this is indeed illegal.
You cannot use function local types as template arguments, because it
would make instantiation very evil.
14.3.1 Template type arguments [temp.arg.type]
1 A template-argument for a template-parameter which is a type shall be a type-id.
2 A local type, a type with no linkage, an unnamed type or a type compounded
from any of these types shall not be used as a template-argument for a
template type-parameter.
Why would you ever want to use a local type as a template argument?
However, when you say outer scope, you are wrong.
You can use a type declared in any scope, except the local one, as a
template argument.
IE nested classes, nested classes in a different namespace, whatever.
--Dan
More information about the Python-list
mailing list