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

Local type cannot be parametr of template?

753 views
Skip to first unread message

Jan Bares

unread,
Oct 6, 2000, 3:00:00 AM10/6/00
to
Can someone tell me if this is correct (according to ANSI), and if not, what
is the reason? The code is just ilustrative.

template <class T>
T ReturnMe(T t) {return t;}

void Test()
{
struct SomeS
{
int i;
};

SomeS s;

s = ReturnMe(s);
}


Regards, Jan
--

Jan Bares
(remove no.spam from my email address)
JPCAD Graphics Engine developer, surf to http://www.antek.cz

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]


maxim

unread,
Oct 6, 2000, 3:00:00 AM10/6/00
to
No, local type is not valid alltogather. (stuck in Java :) ?)
max.

Greg Comeau

unread,
Oct 6, 2000, 3:00:00 AM10/6/00
to
In article <8rkf1t$2dc2$1...@news.vol.cz>,

Jan Bares <jan....@antek.cz.no.spam> wrote:
>Can someone tell me if this is correct (according to ANSI), and if not, what
>is the reason? The code is just ilustrative.
>
>template <class T>
>T ReturnMe(T t) {return t;}
>
>void Test()
> {
> struct SomeS
> {
> int i;
> };
>
> SomeS s;
>
> s = ReturnMe(s);
> }

Comeau C/C++ 4.2.44 (Sep 30 2000 11:56:08) for _MS_WINDOWS_x86_BETA3
Copyright 1988-2000 Comeau Computing. All rights reserved.

"LT.C", line 14: error: a template argument may not reference a local type
s = ReturnMe(s);
^

Of which ANSI agrees. Why? A different example might help address that:
http://www.comeaucomputing.com/techtalk/templates/#stringliteral

- Greg
--
Comeau Computing / Comeau C/C++ ("so close" 4.2.44 betas starting)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: com...@comeaucomputing.com / WEB: http://www.comeaucomputing.com

Brian McNamara!

unread,
Oct 6, 2000, 3:00:00 AM10/6/00
to
"Jan Bares" <jan....@antek.cz.no.spam> once said:
>Can someone tell me if this is correct (according to ANSI), and if not, what
>is the reason? The code is just ilustrative.
>
>template <class T>
>T ReturnMe(T t) {return t;}
>
>void Test()
> {
> struct SomeS
> {
> int i;
> };
>
> SomeS s;
>
> s = ReturnMe(s);
> }

Not legal. From 14.3.1, paragraph 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.

[Example:
template <class T> class X { /* ... */ };
void f()
{
struct S { /* ... */ };
X<S> x3; // error: local type used as template-argument
X<S*> x4; // error: pointer to local type used as template-argument
}
end example]

I think linkage is the motivation for the rule.

--
Brian McNamara

David M. Lee

unread,
Oct 6, 2000, 3:00:00 AM10/6/00
to
"Brian McNamara!" wrote:
> Not legal. From 14.3.1, paragraph 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.
>
> [Example:
> template <class T> class X { /* ... */ };
> void f()
> {
> struct S { /* ... */ };
> X<S> x3; // error: local type used as template-argument
> X<S*> x4; // error: pointer to local type used as template-argument
> }
> end example]
>
> I think linkage is the motivation for the rule.
>
> --
> Brian McNamara

Does that mean that the following is illegal?

typedef struct { /* ... */ } S;

template <class T> class X { /* ... */ };

template <class T> void b (T t) { /* ... */ };

void f ()
{
S s1;
X<S> x1; // is this legal???
b (s1); // how bout this?
}

dave

maxim

unread,
Oct 6, 2000, 3:00:00 AM10/6/00
to
BIG correction:
local functions are not allowed, local types are OK, but cannot be used to
instanciate a template


14.3.1
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. [Example:
template <class T> class X { /* ... */ };
void f()
{
struct S { /* ... */ };
X<S> x3; // error: local type used as template-argument
X<S*> x4; // error: pointer to local type used as template-argument
}

-end example] [Note: a template type argument may be an incomplete type
(3.9). ]

Greg Comeau

unread,
Oct 7, 2000, 12:31:29 AM10/7/00
to
In article <39DE4AA5...@hotmail.com>,

David M. Lee <leed...@hotmail.com> wrote:
>"Brian McNamara!" wrote:
>> Not legal. From 14.3.1, paragraph 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.
>> .......

>> I think linkage is the motivation for the rule.
>
>Does that mean that the following is illegal?
>
>typedef struct { /* ... */ } S;
>
>template <class T> class X { /* ... */ };
>template <class T> void b (T t) { /* ... */ };
>
>void f ()
>{
> S s1;
> X<S> x1; // is this legal???
> b (s1); // how bout this?
>}

The linkage of S is fine, and so therefore the linkage of
the unnamed struct is too, and so therefore the instantiation
is fine.

- Greg
--
Comeau Computing / Comeau C/C++ ("so close" 4.2.44 betas starting)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: com...@comeaucomputing.com / WEB: http://www.comeaucomputing.com

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]

Brian McNamara!

unread,
Oct 7, 2000, 3:00:00 AM10/7/00
to
"David M. Lee" <leed...@hotmail.com> once said:
>"Brian McNamara!" wrote:
>> Not legal. From 14.3.1, paragraph 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.

>Does that mean that the following is illegal?

>typedef struct { /* ... */ } S;

>template <class T> class X { /* ... */ };
>template <class T> void b (T t) { /* ... */ };

>void f ()
>{
> S s1;
> X<S> x1; // is this legal???
> b (s1); // how bout this?
>}

I think they are both legal. Since the struct is declared in a typedef,
it does have a name for linkage purposes (S), and it it not a local
type. g++ also compiles it happily.

--
Brian McNamara

Thaddeus L Olczyk

unread,
Oct 7, 2000, 3:00:00 AM10/7/00
to
On 6 Oct 2000 16:15:31 -0400, com...@panix.com (Greg Comeau) wrote:
>
>Comeau C/C++ 4.2.44 (Sep 30 2000 11:56:08) for _MS_WINDOWS_x86_BETA3
>Copyright 1988-2000 Comeau Computing. All rights reserved.
>
>"LT.C", line 14: error: a template argument may not reference a local type
> s = ReturnMe(s);
> ^
>
>Of which ANSI agrees. Why? A different example might help address that:
>http://www.comeaucomputing.com/techtalk/templates/#stringliteral
>
>- Greg

I haven't read your page yet.
But I'm curoius about something about the reasons that local classes
can't instantiate templates. There is a simple work around using an
"any" type class.

Eg:
Typically I would want to use local classes to wrap functors for stl
algorithms. The wotk around is to define two classes:

// This is going to be the base class for any local functors.
template <class T,class U>
class UnaryFunction:public unary_function<T,U>
{
public:
virtual T operator()(const U &u) const=0;
};

// This is going to be the wrapper for local functors.
template<class T,class U>
class AnyUnaryFunction
{
UnaryFunction<T,U> &f;
public:
AnyUnaryFunction(UnaryFunction<T,U> &g):f(g){};
T operator()(const U &u) const
{
return f(u);
}
};

void foo(void)
{
class local_functor:public UnaryFunction<bool,MyClass>
{
public:
bool operator()(const MyClass &m) const
{
return ...;
}
} lf;
pos=find_if(a.begin(),a.end(),
AnyUnaryFunction<bool,MyClass>(lf));
...

So what abuses can you do instantiating templates with local classes
that isn't also encountered with the technique above?

James Kanze

unread,
Oct 9, 2000, 3:00:00 AM10/9/00
to
Thaddeus L Olczyk wrote:

> // This is going to be the base class for any local functors.
> template <class T,class U>
> class UnaryFunction:public unary_function<T,U>
> {
> public:
> virtual T operator()(const U &u) const=0;
> };

I use this technique a lot, but I generally have a virtual destructor
as well. Typically, no instances will be allocated on the heap, so no
instances will be deleted, so there will be no problem. But why take
the risk?

--
James Kanze mailto:ka...@gabi-soft.de
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelhüttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

0 new messages