Pete Kirkham Pete Kirkham - 3 months ago 20
C++ Question

Overloading operator<< to accept a template function

I'm trying to be able to write an extensible grammar using functions, but can't seem to find the right syntax for accepting a template function. I'm using Visual C++ 2008. It will accept a variable of the same type as the template function, or a similar non-template function, but not the template function itself.


Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) ( line
***
)


class Grammar {
friend Grammar operator << ( const Grammar& lhs, const char* rhs ) {
return lhs; // append rhs to grammar
}
template<typename T>
friend Grammar operator << ( const Grammar& lhs, T (*rhs) () ) {
return lhs; // append rhs() to grammar
}
};

template<typename T>
class ExpressionParticle {
};

template<typename T>
ExpressionParticle<T> Expression () ;

ExpressionParticle<int> ExpressionInt ();

int _tmain ( int argc, _TCHAR *argv[] )
{
ExpressionParticle<int> (*p)();

p = Expression<int>;

Grammar() << "p";
Grammar() << p;
Grammar() << ExpressionInt;
Grammar() << Expression<int>; // ***


What is the type of
Expression<int>
if it is not the type of p in above? How is its type different to the type of
ExpressionInt
.

Answer

Your code looks OK to me, and g++ is fine with that too. This seems to be weird overload resolution bug in Visual Studio. VS2005 seems to have the same problem. A possible workaround is (tested with VS2005):

template<class T>
T id(T t)  {return t; }
int main ()
{
    ExpressionParticle<int> (*p)();

    p = Expression<int>;

    Grammar() << "p";
    Grammar() << p;
    Grammar() << ExpressionInt;
    Grammar() << id(Expression<int>); // ***
}
Comments