Boost logo

Boost :

From: r.lichtenberger_at_[hidden]
Date: 2003-02-12 10:19:00

Hello boosters,

I've justed stumbled across a problem when using the BOOST_CLASS_TEST_CASE

macro in a special way.
I am using class based test cases and want to run every testcase from a
clean environment, i.e. I need to create a new instance of my test class
for every test case.
To keep the code simple and clean I wrote myself a function that returns
the required shared_ptr on a test instance:

--- SNIP ---
boost::shared_ptr<MyTest> testInstance()
    return boost::shared_ptr<MyTest>(new MyTest());

test_suite* init_unit_test_suite( int argc, char * argv[] ) {
    test_suite* test= BOOST_TEST_SUITE( "My Unit Test" );

    test->add(BOOST_CLASS_TEST_CASE(&MyTest::test1, testInstance()));
    test->add(BOOST_CLASS_TEST_CASE(&MyTest::test2, testInstance()));
    test->add(BOOST_CLASS_TEST_CASE(&MyTest::test3, testInstance()));

    return test;
--- SNIP ---

This works perfectly well under Windows using the Microsoft compiler. On
Linux, using gcc (tried 2.96 and 3.2) it fails. The reason it fails is,
because the return value of testInstance() is put into a temporary (is
that the correct term?) variable and the test framework needs a
shared_ptr&. This reference ist not const at the moment, thus the (gcc)
compiler refuses to compile.
The problem can be illustrated this:

--- SNIP ---
class Object {

void test(Object& obj) {

Object makeObject() {
        return Object();

void foo() {
        test(makeObject()); // fails
        Object obj = makeObject();
        test(obj); // works
--- SNIP ---

Although test(makeObject()) should IMHO be just the same as the test(obj)
stuff, the former fails to compile.

I've tried to make the shared_ptr<UserTestCase> const in
unit_test_suite.hpp and succeeded (needed to add "const" in two

May I request that this fix is integrated into the base source tree?

Boost list run by bdawes at, gregod at, cpdaniel at, john at