Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass method with template arguments to a macro

Tags:

I am unable to use Google Test's ASSERT_THROW() macro in combination with multiple template arguments. Consider that I want to make sure that construction of Matrix<5,1> throws:

ASSERT_THROW(Matrix<5,1>(), std::runtime_error); 

(this example doesn't make a lot of sense, of course this shoud not throw, but it is what stayed after simplifying what I had.)

I get this output from MS VC++ 2008:

warning C4002: too many actual parameters for macro 'ASSERT_THROW' error C2143: syntax error : missing ',' before ';' 

Whereas there are no problems with:

ASSERT_THROW(Matrix<1>(), std::runtime_error); 

How can I overcome this problem?

Thanks!

like image 299
Philipp Avatar asked Dec 21 '10 06:12

Philipp


People also ask

How do you pass a parameter to a template in C++?

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

Can a template be considered as a macro?

A lot of Excel users are confused about when to use macros and when to create templates. A macro is a recording of formatting changes and other steps that can be replayed quickly. A template is a pre-formatted spreadsheet with headers and formulas – awaiting your data.

Can we pass Nontype parameters to templates?

Template classes and functions can make use of another kind of template parameter known as a non-type parameter. A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument.

How do you use macros in arguments?

Function-like macros can take arguments, just like true functions. To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like. The parameters must be valid C identifiers, separated by commas and optionally whitespace.


2 Answers

the problem is the extra comma, you will need to protect it from the macro. Try

ASSERT_THROW((Matrix<5,1>()), std::runtime_error); 
like image 87
tletnes Avatar answered Oct 11 '22 13:10

tletnes


#define COMMA , ASSERT_THROW(Matrix<5 COMMA 1>(), std::runtime_error); 

Edit: @tletnes answer is simpler, however this one will work even if the macro parameter used as a non-expression. For example:

BOOST_FOREACH(std::pair<int COMMA int>& v, myVec) { } // works BOOST_FOREACH((std::pair<int, int>)& v, myVec) { } // fails 

More edit: The macro COMMA is already defined in boost:

#include <boost/preprocessor/punctuation/comma.hpp> ASSERT_THROW(Matrix<5 BOOST_PP_COMMA() 1>(), std::runtime_error); BOOST_FOREACH(std::pair<int BOOST_PP_COMMA() int>& v, myVec) { } 
like image 32
Yakov Galka Avatar answered Oct 11 '22 13:10

Yakov Galka