- 论坛徽章:
- 2
|
本帖最后由 OwnWaterloo 于 2015-11-18 19:40 编辑
回复 4# windoze
目前还好。。。 反正是写测试时碰到的。。。 即使绕不过去大不了不测了
大概情况是这样。。。
1. 有几个宏。。。
MODULE_NS
MODULE_NS_ENTER
MODULE_NS_LEAVE
如果MODULE_NS没有定义就有一个默认值
2. 后续代码在合适的地方会用这些宏
MODULE_NS_ENTER
... // 这里面定义的一些宏,因为不知道它们会在何处展开,所以会使用 MODULE_NS::detail::some_thing 之类的
MODULE_NS_LEAVE
如果对默认放的名字空间不满意, 就在包含module.hpp前定义这几个宏,把内容放到别的名字空间里去。
3. 测试使用这几个宏
本来应该是分几个cpp单独测的。
- // test_ns_global.cpp
- #define MODULE_NS
- #define MODULE_NS_ENTER
- #define MODULE_NS_LEAVE
- #include "module.hpp"
复制代码
- // test_ns.cpp
- #define MODULE_NS ::ns
- #define MODULE_NS_ENTER namespace ns {
- #define MODULE_NS_LEAVE }
- #include "module.hpp"
复制代码
- // test_ns_nested.cpp
- #define MODULE_NS ::ns::util
- #define MODULE_NS_ENTER namespace ns { namespace util {
- #define MODULE_NS_LEAVE } }
- #include "module.hpp"
复制代码 test_ns_global.cpp, test_ns.cpp, test_ns_nested.cpp 里面显然还要包含一个真正写测试的文件, 不然重复写这么多次不科学。
我觉得这样文件太多很烦。。。 就都放在一个test_ns.cpp里了。
- // test_ns.cpp
- #ifndef ONLEAVE_TEST_NS_NAME
- #include <assert.h>
- #include <typeinfo>
- // include ...
- // 包含自己3次。。。
- #define MODULE_NS
- #define MODULE_NS_ENTER
- #define MODULE_NS_LEAVE
- #define MODULE_TEST_NS_NAME global
- #include "test_ns.cpp"
- #define MODULE_NS ::ns
- #define MODULE_NS_ENTER namespace ns {
- #define MODULE_NS_LEAVE }
- #define MODULE_TEST_NS_NAME ns
- #include "test_ns.cpp"
- #define MODULE_NS ::ns::util
- #define MODULE_NS_ENTER namespace ns { namespace util {
- #define MODULE_NS_LEAVE } }
- #define MODULE_TEST_NS_NAME nested
- #include "test_ns.cpp"
- int main() {}
- #else /* defined(MODULE_TEST_NS_NAME)*/
- // 实际测试用的代码
- #endif
复制代码 然后就中招了。。。
如果gcc不报错,但选错了名字空间的话, 测试应该全都能通过(毕竟所有名字空间里的功能都是一样的。。。)。。。
但有测试里面有输出typeid(x).name(), 所以仔细看还是能看出来。。。 |
|