Skip to main content
gamedev.net
🔒 Locked

C++: Template problem

Started by Mayrel Nov 19, 2004 at 6:59 AM 8 replies 2k views
Original Post
Mayrel
Mayrel

struct Foo
{
  template<typename T = void>
  struct Wombat {};
};

template<typename Base>
struct Bar: public Base
{
  struct Fiddle: Wombat<int> {};
};
How do I tell the compiler that 'Wombat' is a template? I want to avoid having to copy the template declaration in each subtype.
kuphryn
kuphryn
Interesting.

You want something like this?

struct Fiddle: Wombat {};

Wombat as is a struct temptate without a type. You can make its parameter default to int, making it a int template struct.

Kuphryn


rdfodra
rdfodra
I think you should get rid of the Foo struct since you're not using it:

template
struct Wombat {
T something;
};

But I don't see any advantage in what you're trying to do.
Ricardo, Brazil
Mayrel
Mayrel
Quote:
Original post by rdfodra
But I don't see any advantage in what you're trying to do.

Well, how could you? That's just a simplified version of what's not working. Here's some more realistic code:
struct EmptyVertex{  // Describes a vertex attribute. For use by vertex array  // code.  template<typename Type = void, int Size = 0, int Offset = 0>  struct empty_type {    static const int type =  gl_type<Type>::type;    static const int size = Size;    static const int offset = Offset;  };  // The default is that vertex attributes aren't there.  struct position_type:        public empty_type<> {};  struct normal_type:          public empty_type<> {};  struct color_type:           public empty_type<> {};  struct secondary_color_type: public empty_type<> {};  struct tex_coord_type:       public empty_type<> {};  struct weight_type:          public empty_type<> {};};// This adds a position attribute to an existing vertex type,// which may be the EmptyVertex type.template<typename Type = float,         int size = 3,         typename Base = EmptyVertex>struct PositionVertex : public Base{  // The position  Type position[size];  // 'Override' the position_type member with the correct   // information about this attribute.  struct position_type: public empty_type<Type, size, offsetof(PositionVertex, position) > {};  // The problem is that as empty_type is a member of  // EmptyVertex, the compiler doesn't appear to know that it's  // a template. It works fine if empty_type is defined outside  // EmptyVertex, but I'd rather not have to do that.};
Sharlin
Sharlin
If I understood it correctly, you just have to use empty_type's full name so that the compiler can find it, ie. qualify it with "EmptyVertex::" :

struct PositionVertex : public Base{  struct position_type: public EmptyVertex::empty_type<Type, size, offsetof(PositionVertex, position) > {};};

Fruny
Fruny
Have you tried struct position_type: public typename Base::empty_type {}; ?

Or even possibly struct position_type: public typename Base::template empty_type {}; (e.g. if you get parse errors about < and >)


I don't have my C++ templates book nearby (and am too lazy to check in the standard).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Jingo
Jingo
Can't have typename in a base class specification list Fruny.
snk_kid
snk_kid
//....template<typename Type = float,         int size = 3,         typename Base = EmptyVertex>struct PositionVertex : public Base {	Type position[size];		struct position_type : public Base::template empty_type<Type, size, offsetof(PositionVertex, position) > {};};
Mayrel
Mayrel
Quote:
Original post by snk_kid
*** coolness ***

Excellent! Exactly right. I thank you. [smile]
Fruny
Fruny
Oh well, I had it almost right. Would you believe that the second 'typename' was a cut-and-past error? No... I guess not. [grin] [razz] [rolleyes]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.