The following does not compile:
class Foo {
public:
Foo( boost::shared_ptr< Bar > arg );
};
// in test-case
boost::shared_ptr< Bar > bar;
BOOST_CHECK_THROW( Foo( bar ), std::logic_error ); // compiler error here
The implementation of Bar does not matter. The compiler complains, that Foo does not have an appropriate default constructor (VC++ 2005). If I add a default constructor, it works, and it actually gets called. Why does this statement require a default constructor?
This occurs because
BOOST_CHECK_THROWis a macro, andFoo(bar)is being expanded to a statement. The compiler sees this statement and interprets it as a variable declarationFoo bar;which requires a default constructor.The solution is to give the variable a name:
In other words
BOOST_CHECK_THROWwill expand to something likeand the compiler is interpreting
Foo(bar);as the declaration of a variable called bar. One can check this with a simple program:which gives the following errors with g++