By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

tricky template syntax question

P: 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
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
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.