C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

enums and the curiously recurring template pattern

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Derrick Bass
Guest





PostPosted: Fri Apr 23, 2004 11:14 pm    Post subject: enums and the curiously recurring template pattern Reply with quote



I was trying to use the "curiously recurring template pattern" with
enums for compile time constants, but with the following code gcc gives
me an error:
template< class T>
struct X {
void f() { T::ddim; } // works
enum { dim = T::ddim }; // compile-time error
};

struct Y : public X<Y> {
enum { ddim = 2 };
};

int main() {
Y y;
y.f();
}

The error is
enum.cc: In instantiation of `X<Y>':
enum.cc:7: instantiated from here
enum.cc:4: error: incomplete type `Y' does not have member `ddim'
enum.cc:4: error: enumerator value for `dim' not integer constant

I don't have another compiler to test, but gcc is pretty standards
compliant these days so I assume the problem is my code.

Why is the use of T::ddim in the function f() allowed but not in the
enum?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ben Hutchings
Guest





PostPosted: Wed Apr 28, 2004 9:26 am    Post subject: Re: enums and the curiously recurring template pattern Reply with quote



Derrick Bass wrote:
Quote:
I was trying to use the "curiously recurring template pattern" with
enums for compile time constants, but with the following code gcc gives
me an error:
template< class T
struct X {
void f() { T::ddim; } // works
enum { dim = T::ddim }; // compile-time error
};

struct Y : public X enum { ddim = 2 };
};

int main() {
Y y;
y.f();
}

The error is
enum.cc: In instantiation of `X<Y>':
enum.cc:7: instantiated from here
enum.cc:4: error: incomplete type `Y' does not have member `ddim'
enum.cc:4: error: enumerator value for `dim' not integer constant

I don't have another compiler to test, but gcc is pretty standards
compliant these days so I assume the problem is my code.

Why is the use of T::ddim in the function f() allowed but not in the
enum?

A base class must be defined before a class derived from it is
defined. In this case the base class of Y is defined by implicit
instantiation of class template X.

According to the standard, in section 14.7.1 paragraphs 1-2, when a
class template is instantiated, the *declarations* of member functions
are instantiated, but not their definitions or default arguments. The
*definitions* of member functions are not instantiated until those
member functions are used or explicitly instantiated.

14.7.1/1 appears to specify that the enumeration should not be
instantiated until it is used, but I'm not sure that I'm reading it
correctly. VC++ 7.1 and Comeau C++ 4.3.3 beta both complain about
the enumeration definition.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dave Moore
Guest





PostPosted: Wed Apr 28, 2004 7:51 pm    Post subject: Re: enums and the curiously recurring template pattern Reply with quote



Derrick Bass <dernospamrick (AT) nospam (DOT) cco.caltech.edu> wrote

Quote:
I was trying to use the "curiously recurring template pattern" with
enums for compile time constants, but with the following code gcc gives
me an error:
template< class T
struct X {
void f() { T::ddim; } // works
enum { dim = T::ddim }; // compile-time error
};

struct Y : public X enum { ddim = 2 };
};

int main() {
Y y;
y.f();
}

The error is
enum.cc: In instantiation of `X<Y>':
enum.cc:7: instantiated from here
enum.cc:4: error: incomplete type `Y' does not have member `ddim'
enum.cc:4: error: enumerator value for `dim' not integer constant

I don't have another compiler to test, but gcc is pretty standards
compliant these days so I assume the problem is my code.

Why is the use of T::ddim in the function f() allowed but not in the
enum?

I don't have time to check the Standard for the "real" answer, but I
ran into a similar problem a few weeks with gcc 3.3.1, and it had to
do with "second template instatiation" .. or something that sounded
vaguely like that. Anyway, the problem was "solved" (meaning it went
away <grin>) by the update to gcc 3.4.0 ... the release version just
came out a week or so ago.

HTH, Dave Moore

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.