I am using Boost 1.46 with Turtle lib 1.2.4 and compiler from Visual Studio Express 2013. I have following class to MOCK:
struct IPredicate
{
virtual ~IPredicate() {}
virtual bool operator()(float value) = 0;
};
When I mock operator() with MOCK_NON_CONST_METHOD:
MOCK_BASE_CLASS(MockPredicate, IPredicate)
{
MOCK_NON_CONST_METHOD(operator(), 1, bool(float), id)
};
I got bunch of compiler errors, e.g. syntax error 'operator ' and so on. But when I mock it with MOCK_NON_CONST_METHOD_EXT:
MOCK_BASE_CLASS(MockPredicate, IPredicate)
{
MOCK_NON_CONST_METHOD_EXT(operator(), 1, bool(float), id)
};
everything is ok and works perfectly! According to http://turtle.sourceforge.net/turtle/reference.html MOCKS with EXT suffix are for "compilers without support for variadic macros", but the one I am using has support (checked it with these examples: http://msdn.microsoft.com/en-us/library/ms177415.aspx ). The rest of the documentation isn't really clear about this case.
Is anyone able to explain me what's the case here? Why I have the errors when I don't use EXT suffixed MOCK version?
The stickler’s answer would be that generally there are no guarantees with respect to variadic macros, as variadic macros are non-standard in C++03 (but are standard in C++11). So if you have a method which avoids variadic macros you should definitely use it instead the one with the variadic macros.
Practically though it is very likely that the turtle library hasn’t been extensively tested with msc and is simply relying on one of the non-standard gcc extensions for the macros. The extensions are discussed on the Variadic Macros http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html page. Specifically, for the turtle library to be portable for all C99 conformant compilers only __VA_ARGS__
could be used.
With macros, when you are after the root case - use the /P switch for msc (Preprocess to a File) to generate an .i
file with the pre-processors directives expanded, where you can check what's it unhappy about.
Update. As I finished spanning this long story, I decided to quickly download the turtle and check how the macro is defined. And as I did, I discovered that this is simply a sad case of unmaintained documentation. Running grep on the library includes I couldn't find MOCK_NON_CONST_METHOD
defined at all. That's why you are getting syntax errors. Another reason to avoid macros - clarity and sanity of C++ error messages.