=======================
template<class C>
class Template {};
int main(void)
{
class Inner {};
Template<Inner> ti;
return 0;
}
=======================
My compiler (GCC 3.2.2) tells this:
tmpl.cc: In function `int main()':
tmpl.cc:8: template-argument `main()::Inner' uses local type `main()::Inner'
tmpl.cc:8: ISO C++ forbids declaration of `ti' with no type
(Please don't tell me that local class beaks OO design,
I don't believe this).
This would be so nice to create some STL-predicates
that are to used just 'here and now' as local classes.
Is this an ISO issue ? Does it work in windows ?
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
The reason local classes can't be used as arguments to templates is that
the arguments are required to have external linkage, whereas local
classes have no linkage. Yes, it's an ISO issue, if you by that mean
it's in the standard. No, it doesn't work on Windows (or any other
particular platform), if the compiler is conformant.
However, there exists a formal proposal to allow local classes to be
used as template parameters, even though they still will have no linkage
(http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1427.pdf).
Regards,
Terje
> Why local class cannot be template argument ?
I guess because they never got around to removing the restriction :).
It's got something to do with local classes having internal linkage, but
they could always remove this restriction.
But the killer enhancement: local classes should be able to access local
variables of the function.
--
+++++++++++
Siemel Naran
IMHO, theproblem is not with the local class being a template, but
with the definition of a class *inside* main. I don;t think you can do
that.
>>class Inner {}; //problem is here.
>> Template<Inner> ti; //not here.
>> return 0;
Regards,
Dhruv.
Probably the restriction is too severe. This doesn't directly
address the OPs question, but should provide some words on the issue:
http://www.comeaucomputing.com/techtalk/templates/#stringliteral
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
>
> IMHO, theproblem is not with the local class being a template, but
> with the definition of a class *inside* main. I don;t think you can do
> that.
>
> >>class Inner {}; //problem is here.
> >> Template<Inner> ti; //not here.
> >> return 0;
>
> Regards,
> Dhruv.
>
Ok, sorry that's not the problem (Surprise for me!!! I did not know
you could do that i.e. Define a class within a function.). Maybe what
the othe posters have said is true. I believe it is.
-Dhruv.
They can - as long as the variables are static. I think access to
automatic variables would require closures, and adding them to the
language would be a very big (and useful) change.