在编写测试套件时,我需要提供一个实现operator<<(std::ostream&...
供 Boost 单元测试使用。
这有效:
namespace theseus { namespace core {
std::ostream& operator<<(std::ostream& ss, const PixelRGB& p) {
return (ss << "PixelRGB(" << (int)p.r << "," << (int)p.g << "," << (int)p.b << ")");
}
}}
这没有:
std::ostream& operator<<(std::ostream& ss, const theseus::core::PixelRGB& p) {
return (ss << "PixelRGB(" << (int)p.r << "," << (int)p.g << "," << (int)p.b << ")");
}
显然,当 g++ 尝试解决运算符的使用时,第二个并未包含在候选匹配中。为什么(什么规则导致这个)?
代码调用operator<<
深入 Boost 单元测试框架,但测试代码如下:
BOOST_AUTO_TEST_SUITE(core_image)
BOOST_AUTO_TEST_CASE(test_output) {
using namespace theseus::core;
BOOST_TEST_MESSAGE(PixelRGB(5,5,5)); // only compiles with operator<< definition inside theseus::core
std::cout << PixelRGB(5,5,5) << "\n"; // works with either definition
BOOST_CHECK(true); // prevent no-assertion error
}
BOOST_AUTO_TEST_SUITE_END()
作为参考,我正在使用 g++ 4.4(尽管目前我假设此行为符合标准)。