0

I am trying to implement a templated class like below.

   struct Ciccio2 : public T1, public T2, public T3, public T4
   {
     template<typename TR>
     int get(const string& s) const
     {
       return TR::operator()(s);
     }
   };

All templated argument are like the following sample class

   struct AA{ 
     int operator()(const string& s) { return 1;}
   };

I am trying also to have a global extractor function but when I use the template function below g++ gives me a build error saying

   template<class TA, class T1, class T2, class T3, class T4>
   int extract(const Ciccio2<T1,T2,T3,T4>& obj, const string& s)
   {
     return obj.get<TA>(s);
   }

the code below doesn't buld saying

expected primary expression before > token

Is it correct what I am trying to implement?

Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173

2 Answers2

1

Since obj is a type-dependent expression, the compiler won't use name lookup to determine whether obj.get is an object, function, type, or template. Unless you force it to do otherwise, the compiler assumes it is an object or function for syntax analysis. Next it sees the < less than operator (not the < beginning a template argument list), and soon afterward gets confused. You need:

return obj.template get<TA>(s);

See also the question Where and why do I have to put the template and typename keywords?

Community
  • 1
  • 1
aschepler
  • 70,891
  • 9
  • 107
  • 161
0

Unless TR is one of T1, T2, T3, and T4, the line:

   return TR::operator()(s);

should be:

   return TR().operator()(s);

or

   return TR()(s);

Since operator() cannot be a static member function.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • But if `TR::operator()` is a member of `*this`, it can be valid with the implicit `this->`. (At least, I think it's valid without the explicit `this->`...) – aschepler Jun 23 '14 at 18:54