Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

c++ - pass method with template arguments to a macro

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#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) { }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...