This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Name lookup in templates
- From: Michael Heissmeier <michael63 at gmx dot net>
- To: gcc-help at gcc dot gnu dot org
- Date: Sat, 29 Jul 2006 01:17:23 +0200 (CEST)
- Subject: Name lookup in templates
Hello,
the following code snippet compiles under gcc 3.2.3 but fails to do so using
gcc 4.1.0. It is not clear to me whether this is a problem with gcc or with
my code.
The compiler tells me that it cannot convert C to an int. It seems that TFunc's
call to A::func is bound at definition time although this call depends on the
template parameter. I therefore would expect it to bind during instantiation.
--------------------------------------------
namespace A {
int func (int c) { return (c + 1); }
}
template <typename Type>
int TFunc (Type t) { return A::func (t); }
class C
{
public:
int myint;
};
namespace A {
int func (C c) { return (c.myint + 1); }
}
int main ()
{
C x;
x.myint = 2;
int y = TFunc (x);
}
--------------------------------------------------------
The reason for this code layout is that the first definition of func
and the template reside in a header which should be included in many
source files.
The definition of Class C happens in an implementation where
namespace A is reopened in order to add an according definition for func.
It would be nice if there were a solution without getting rid of the
namespace which prevents us from having to prefix lots of names.
Any suggestions are welcome.
Michael Heissmeier