Boost 测试失败,命名空间内有枚举 类
Boost test fails with enum classes inside namespaces
如果你为 C++11 enum class
定义了一个 operator <<
,那么你可以成功地将它与 Boost 的单元测试库一起使用。
但是,如果将 enum class
放在 namespace
中,Boost 代码将不再编译。
为什么将 enum class
放在 namespace
中会阻止它工作?它与 std::cout
两种方式都可以正常工作,所以这肯定意味着 operator <<
是正确的?
下面是一些演示该问题的示例代码:
// g++ -std=c++11 -o test test.cpp -lboost_unit_test_framework
#include <iostream>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE EnumExample
#include <boost/test/unit_test.hpp>
// Remove this namespace (and every "A::") and the code will compile
namespace A {
enum class Example {
One,
Two,
};
} // namespace A
std::ostream& operator<< (std::ostream& s, A::Example e)
{
switch (e) {
case A::Example::One: s << "Example::One"; break;
case A::Example::Two: s << "Example::Two"; break;
}
return s;
}
BOOST_AUTO_TEST_CASE(enum_example)
{
A::Example a = A::Example::One;
A::Example b = A::Example::Two;
// The following line works with or without the namespace
std::cout << a << std::endl;
// The following line does not work with the namespace - why?
BOOST_REQUIRE_EQUAL(a, b);
}
如果你想使用ADL,你需要在命名空间中定义运算符。
#include <iostream>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE EnumExample
#include <boost/test/unit_test.hpp>
namespace A {
enum class Example {
One,
Two,
};
std::ostream& operator<< (std::ostream& s, Example e)
{
switch (e) {
case A::Example::One: s << "Example::One"; break;
case A::Example::Two: s << "Example::Two"; break;
}
return s;
}
} // namespace A
BOOST_AUTO_TEST_CASE(enum_example)
{
A::Example a = A::Example::One;
A::Example b = A::Example::Two;
// The following line works with or without the namespace
std::cout << a << std::endl;
// The following line does not work with the namespace - why?
BOOST_REQUIRE_EQUAL(a, b);
}
如果你为 C++11 enum class
定义了一个 operator <<
,那么你可以成功地将它与 Boost 的单元测试库一起使用。
但是,如果将 enum class
放在 namespace
中,Boost 代码将不再编译。
为什么将 enum class
放在 namespace
中会阻止它工作?它与 std::cout
两种方式都可以正常工作,所以这肯定意味着 operator <<
是正确的?
下面是一些演示该问题的示例代码:
// g++ -std=c++11 -o test test.cpp -lboost_unit_test_framework
#include <iostream>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE EnumExample
#include <boost/test/unit_test.hpp>
// Remove this namespace (and every "A::") and the code will compile
namespace A {
enum class Example {
One,
Two,
};
} // namespace A
std::ostream& operator<< (std::ostream& s, A::Example e)
{
switch (e) {
case A::Example::One: s << "Example::One"; break;
case A::Example::Two: s << "Example::Two"; break;
}
return s;
}
BOOST_AUTO_TEST_CASE(enum_example)
{
A::Example a = A::Example::One;
A::Example b = A::Example::Two;
// The following line works with or without the namespace
std::cout << a << std::endl;
// The following line does not work with the namespace - why?
BOOST_REQUIRE_EQUAL(a, b);
}
如果你想使用ADL,你需要在命名空间中定义运算符。
#include <iostream>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE EnumExample
#include <boost/test/unit_test.hpp>
namespace A {
enum class Example {
One,
Two,
};
std::ostream& operator<< (std::ostream& s, Example e)
{
switch (e) {
case A::Example::One: s << "Example::One"; break;
case A::Example::Two: s << "Example::Two"; break;
}
return s;
}
} // namespace A
BOOST_AUTO_TEST_CASE(enum_example)
{
A::Example a = A::Example::One;
A::Example b = A::Example::Two;
// The following line works with or without the namespace
std::cout << a << std::endl;
// The following line does not work with the namespace - why?
BOOST_REQUIRE_EQUAL(a, b);
}