472,102 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

tricky template syntax question

1
I have a template class with a function that looks like this:
Expand|Select|Wrap|Line Numbers
  1. template <class Key, int Value, class Tail> 
  2. struct Parser<Typelist< MakePair<Key, Value>, Tail >, EmptyType>
  3. {        
  4.     static inline bool parse(int* value) 
  5.     {        
  6.         return (*value == Value) ? Key::Execute() : Parser<Tail>::parse(value);                                  
  7.     }
  8. };
  9.  
"Key" is a template parameter which specifies a class that has a static member function called "Execute".

This code compiles and runs correctly.

Now I want to make "Execute" template function that takes an int. I changed the definition of "Execute" to be template <int T> bool Execute()
and I changed the above code to be:
Expand|Select|Wrap|Line Numbers
  1. template <class Key, int Value, class Tail> 
  2. struct Parser<Typelist< MakePair<Key, Value>, Tail >, EmptyType>
  3. {        
  4.     static inline bool parse(int* value) 
  5.     {        
  6.         return (*value == Value) ? Key::Execute<Value>() : Parser<Tail>::parse(value);                                  
  7.     }
  8. };
  9.  
Note that I am now tring to pass "Value" as a template parameter to Execute.

This won't compile and I get:

error: invalid operands of types `<unknown type>' and `int' to binary `operator<'
and
In static member function `static bool Parser<Typelist<MakePair<T, U>, Tail>, EmptyType>::parse(int*)':
classTemplates.h:81: error: expected primary-expression before ')' token

Why doesn't this work? Do I need to make the class "Key" a template class too? Everything that I've read says you can have a template member function in a class that is not a template class.

Why doesn't it recognize this as an attempt to pass a template parameter and try and treat it as a binary operator. How can I disambiguate this?
Apr 13 '07 #1
1 1509
weaknessforcats
9,208 Expert Mod 8TB
Yes, you can have a member function template in a class that is itself not a template. For example, this is OK:

class Shape
{
public:
template<class T>
void draw(T arg);
};

template<class T>
void Shape::draw(T arg)
{

}

I'm worried about this:

template <int T> bool Execute()

should this not be:
template <class T> bool Execute() { etc...}

I copied your fragment and diddled with it so ti compiles:

class Key
{
public:
template<class T>
void Execute();
};

template<class T>
void Key::Execute()
{
T data;
}


//Your code:
template <class Key, int Value, class Tail>
struct Parser
//struct Parser<Typelist< MakePair<Key, Value>, Tail >, EmptyType>
{
static inline bool parse(int* value)
{
return (*value == Value) ? Key::Execute<Value>() : Parser<Tail>::parse(value);
}
};
Apr 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Jyrki Keisala | last post: by
8 posts views Thread by daniel.w.gelder | last post: by
5 posts views Thread by Steve Schlesinger | last post: by
2 posts views Thread by toton | last post: by
272 posts views Thread by Peter Olcott | last post: by
2 posts views Thread by adrian.hawryluk | last post: by
9 posts views Thread by Adam Nielsen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.