This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Problem with templates used in a class member.
- To: egcs at cygnus dot com
- Subject: Problem with templates used in a class member.
- From: "Ram'on Garc'ia" <ramon at jl1 dot quim dot ucm dot es>
- Date: Thu, 3 Sep 1998 04:46:10 +0200 (CEST)
A limitation of EGCS is that it cannot compile code where a class contains
a member that uses a template for its type, if this template uses class
members as parameters.
For example, the followin correct code does not compile with EGCS 1.1a:
template <class P, void (P::*member)()>
class x { };
class user {
public:
user() {}
void fn() {}
x<user, fn> a;
};
because the member a of type x<member, fn> uses the parameter fn, that is
declared inside the class uses.
I checked the source code of EGCS to see if I can solve it, but the
problem is not trivial. When EGCS instatiates the template x<user,fn>, the
declaration of the function fn is not in the tree of the class user. EGCS
does not place the declaration of the functions inside the tree of the
class until the declaration of the class ends. A solution could be to
delay template instantation until the rest of the class members have been
placed in the syntax tree of the class. But that would not be a perfect
solution: a template can use as a parameter a member that is also defined
using a template.
Compilers based on the EDG frontend, such as the Intel compiler, IBM
XLC++, and many others use a compromise solution, that allows to use
members as template parameters only after the declaration of the member.
For example:
class user {
public:
user() {}
x<user, fn> a;
void fn() {}
};
would not work, because fn is declared before the template x uses it. This
is not very consistent, since the body of a member function can use
members that have not yet been declared, for example, you can have:
class a {
public:
void fn1() { fn2(); /*we call fn2() that has not been declared*/ }
void fn2() { /* whatever you wish */}
};
Is this behavior compliant with standards?
Ramon.