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! ]
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
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
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). ]
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 ]
>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
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?
> // 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