31 December 2009

Small gotcha when combining Boost.Array, Assign, and Test

When attempting to use Boost.Assign's list_of in expressions like BOOST_CHECK_EQUAL(somearray, list_of(x)(y)(z)), where somearray has a type like boost::array<T,N>, I kept running into funky errors that there was no operator<< defined with the appropriate types. So I'd define one at global scope, but the errors kept arising from the depths of BOOST_CHECK_EQUAL. It turns out that I needed to define such an operator<< in the boost namespace:

namespace boost {

template< typename charT, typename traits, typename T, ::std::size_t N >
::std::basic_ostream& operator<<(
        ::std::basic_ostream &os, const ::boost::array &array)
{
    os << '[' << N << "]{ ";
    ::std::copy(array.begin(),
                array.end(),
                ::std::ostream_iterator(os, " "));
    os << '}';
    return os;
}

}

1 comment:

terry said...

fantastic, just what i needed... after many, many attempts to solve. how did you discover this, and more to the point... WHY does it need it? is there not some way of using a using clause to import it from another namespace?

Subscribe Subscribe to The Return of Agent Zlerich