Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why local class cannot be template argument ?

24 views
Skip to first unread message

Peter A. Kerzum

unread,
May 30, 2003, 4:21:49 PM5/30/03
to
Why local class cannot be template argument ?
Assume this senseless code:

=======================
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! ]

Terje Slettebø

unread,
May 31, 2003, 4:40:39 AM5/31/03
to
"Peter A. Kerzum" <ker...@mail.ru> wrote in message
news:d4039ed4.03053...@posting.google.com...

> Why local class cannot be template argument ?
> Assume this senseless code:
>
> =======================
> 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 ?

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

Siemel Naran

unread,
May 31, 2003, 5:12:22 AM5/31/03
to
"Peter A. Kerzum" <ker...@mail.ru> wrote in message

> 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

Dhruv

unread,
May 31, 2003, 5:12:58 AM5/31/03
to
ker...@mail.ru (Peter A. Kerzum) wrote in message news:<d4039ed4.03053...@posting.google.com>...

> Why local class cannot be template argument ?
> Assume this senseless code:
>
> =======================
> 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 ?

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.

Greg Comeau

unread,
May 31, 2003, 11:25:23 AM5/31/03
to
In article <Q%WBa.104790$cO3.7...@bgtnsc04-news.ops.worldnet.att.net>,

Siemel Naran <Sieme...@KILL.att.net> wrote:
>"Peter A. Kerzum" <ker...@mail.ru> wrote in message
> > 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.

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?

Dhruv

unread,
May 31, 2003, 3:18:40 PM5/31/03
to
dhru...@gmx.net (Dhruv) wrote in message
news:<cf18e89.03053...@posting.google.com>...

> ker...@mail.ru (Peter A. Kerzum) wrote in message
news:<d4039ed4.03053...@posting.google.com>...

>

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

Ben Hutchings

unread,
Jun 4, 2003, 2:02:09 PM6/4/03
to
In article <Q%WBa.104790$cO3.7...@bgtnsc04-news.ops.worldnet.att.net>,

Siemel Naran wrote:
> "Peter A. Kerzum" <ker...@mail.ru> wrote in message
>
> > 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.

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.

0 new messages