The compiler complains about BOOST_CHECK_THROW on the constructor

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 is actually called. Why does this statement require a default constructor?

+3
source share
1 answer

This is because it BOOST_CHECK_THROWis a macro and Foo(bar)extends to an operator. The compiler sees this statement and interprets it as a variable declaration Foo bar;, which requires a default constructor.

, :

BOOST_CHECK_THROW( Foo temp( bar ), std::logic_error );

BOOST_CHECK_THROW -

try
{
    Foo(bar);
    // ... fail test ...
}
catch( std::logic_error )
{
    // ... pass test ...
}

Foo(bar); bar. :

struct Test
{
    Test(int *x) {}
};

int main()
{
    int *x=0;
    Test(x);
    return 0;
}

g++

test.cpp: In function β€˜int main()’:
test.cpp:10: error: conflicting declaration β€˜Test x’
test.cpp:9: error: β€˜x’ has a previous declaration as β€˜int* x’
+9

Source: https://habr.com/ru/post/1735251/


All Articles