Jiew Meng
Jiew Meng

Reputation: 88197

Create an alias to a function

I want to create an alias to CPPUNIT_ASSERT_EQUAL_MESSAGE(string message, T expected, T actual). I tried:

template<class T> void (&_assert)(string, T, T) = &CPPUNIT_ASSERT_EQUAL_MESSAGE;

Not sure if its right at all, but I am getting errors like

Error   1   error C2530: '_assert' : references must be initialized h:\dropbox\sch\cs3202\code\test\testqueryevaluator\testgetcandidatelist.h   22
Error   2   error C2998: 'void (__cdecl &__cdecl _assert)(std::string,T,T)' : cannot be a template definition   h:\dropbox\sch\cs3202\code\test\testqueryevaluator\testgetcandidatelist.h   22

Whats the right syntax?

Upvotes: 0

Views: 159

Answers (3)

eladidan
eladidan

Reputation: 2644

CPPUNIT_ASSERT_EQUAL_MESSAGE is a macro and not a function, and so you can either "wrap" it with an actual definition of an inline function (as a previous answer suggested) or simply #define an alias macro:

#define _assert CPPUNIT_ASSERT_EQUAL_MESSAGE

Of the two, I would choose the wrapper function method so it could be declared in a namespace and to avoid naming conflicts.

Upvotes: 1

Bo Persson
Bo Persson

Reputation: 92261

Just create a forwarding function:

template<class T> 
inline void _assert(const string& message, T expected, T actual) 
{ CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual); }

Upvotes: 3

Arne Mertz
Arne Mertz

Reputation: 24596

Simple put, there is no right syntax, because, as phoeagon noted, that is a macro, no function:

See the reference

Upvotes: 1

Related Questions