Following on from this question, which asks about SFINAE it gives the example of:
template<class T>
std::string optionalToString(T* obj)
{
if (FUNCTION_EXISTS(T->toString))
return obj->toString();
else
return "toString not defined";
}
However, if an object doesn't have, for example, a toString() function, instead of this case returning "toString not defined", even if we can detect whether or not the function exists, the compiler will still throw an error that object has no member named "toString", before highlighting the pointer toString call.
I'd like to be able to do the same operations on objects that come from C++ libraries that have different naming conventions, for example:
if(R_Contains_SetPosition<TemplateObject>::Value)
{
TemplateObject->SetPosition(X,Y);
}
else if(R_Contains_setPosition<TemplateObject>::Value)
{
TemplateObject->setPosition(X,Y); //TemplateObject doesn't have a setPosition defined!
}
Which the code can already do, but the compiler throws an error on whichever statements that call functions that the object doesn't have defined.
Is there some way to get the compiler to accept code (either by re-writing it or modifying compiler flags, preferably the former) that has the ability to call a member function (which in this case won't be run anyway), even if the compiler knows said member function doesn't exist?
Clarification:
This isn't asking how to detect a function exists. I already have this ability., it's asking how can I write code that refers to functions that a object won't necessarily have without the compiler complaining about it?
The system is C++03, so experimental C++14 like solutions won't be valid here.