模板解析 - operator<< not found with boost-test
Template resolution - operator<< not found with boost-test
以下代码工作正常:
#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] );
}
但是,当我将测试用例替换为
BOOST_AUTO_TEST_CASE( test_foo )
{
BOOST_CHECK_EQUAL( foo, foo );
}
然后 operator<<
不再被编译器找到:
/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<> >’)
我希望编译器实例化上面定义的 operator<<
模板。为什么这没有发生/如何解决?
编辑:查看评论,这是 UB - 似乎没有 "good" 解决问题的方法。
将您的 op<<
包裹在 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 );
}
否则它不会为您的实现寻找正确的命名空间。很确定这是 ADL:https://en.cppreference.com/w/cpp/language/adl
以下代码工作正常:
#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] );
}
但是,当我将测试用例替换为
BOOST_AUTO_TEST_CASE( test_foo )
{
BOOST_CHECK_EQUAL( foo, foo );
}
然后 operator<<
不再被编译器找到:
/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<> >’)
我希望编译器实例化上面定义的 operator<<
模板。为什么这没有发生/如何解决?
编辑:查看评论,这是 UB - 似乎没有 "good" 解决问题的方法。
将您的 op<<
包裹在 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 );
}
否则它不会为您的实现寻找正确的命名空间。很确定这是 ADL:https://en.cppreference.com/w/cpp/language/adl