The following code works fine:
#define BOOST_TEST_MODULE TestFoo
#include <boost/test/unit_test.hpp>
#include <boost/dynamic_bitset.hpp>
#include <string>
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T> &v)
{
os << "[ ";
for ( const T& elem : v )
os << elem << ' ';
return os << ']';
}
typedef boost::dynamic_bitset<> BS;
static const std::vector<BS> foo = { BS(std::string("101")) };
BOOST_AUTO_TEST_CASE( test_foo )
{
BOOST_CHECK_EQUAL( foo[0], foo[0] );
}
However, when I replace the test case with
BOOST_AUTO_TEST_CASE( test_foo )
{
BOOST_CHECK_EQUAL( foo, foo );
}
then operator<<
is no longer found by the compiler:
/usr/include/boost/test/test_tools.hpp:326:14: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘const std::vector<boost::dynamic_bitset<> >’)
I would expect the compiler to instantiate the operator<<
template defined above. Why is this not happening / how to fix?
edit: see comment, this is UB - there doesn't appear to be a "good" solution to the problem.
wrap your op<<
in a namespace std {...}
#include <boost/dynamic_bitset.hpp>
#include <boost/test/unit_test.hpp>
namespace std { // THIS LINE
template <typename T, typename... Rest>
std::ostream& operator<<(std::ostream& os, const std::vector<T, Rest...> &v)
{
os << "[ ";
for ( const T& elem : v )
os << elem << ' ';
os << ']';
return os;
}
} // THIS LINE
typedef boost::dynamic_bitset<> BS;
static const std::vector<BS> foo = { BS(std::string("101")) };
BOOST_AUTO_TEST_CASE( test_foo )
{
BOOST_CHECK_EQUAL( foo, foo );
}
Otherwise it's not looking in the right namespace for your implementation. Pretty sure this is ADL: https://en.cppreference.com/w/cpp/language/adl