'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!
Solution 1:[1]
#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) { }
Solution 2:[2]
#define COMMA , may not compile in GCC. Use #define COMMA() , instead. Why can't I add comment to other's post?
Solution 3:[3]
In addition to the answers posted, you could also try
using ColVector5f = Matrix<5,1>;
ASSERT_THROW(ColVector5f(), std::runtime_error);
So that the preprocessor doesn't get confused by the comma.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | wqking |
| Solution 3 | juicedatom |
