带有宏的 C++ 嵌套命名空间
C++ nested namespaces with a macro
本题基于
- C++ namespaces advice
和
我想效仿
namespace foo::bar::baz {
在 C++17 到来之前使用宏。
我的思路是:
#define BOOST_PP_VARIADICS
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq/fold_left.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>
#define OP(s, state, x) BOOST_PP_CAT(state, BOOST_PP_CAT( { namespace, x )) {
#define NS(...) namespace BOOST_PP_SEQ_FOLD_LEFT(OP, BOOST_PP_SEQ_HEAD(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)), BOOST_PP_SEQ_TAIL(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)))
NS(foo, bar, baz)
基于第二个 link,但这给了我:
namespace foo { namespacebar { namespacebaz {
如何在 namespace
和标识符之间添加 space?
编辑:
如果您可以制作一个宏,使 ns(foo::bar::baz)
扩展为 namespace foo { namespace bar { namespace baz {
,那就更好了。
你可以用 BOOST_PP_SEQ_FOR_EACH
做的更简单:
#define BOOST_PP_VARIADICS
#include <boost/preprocessor/variadic/to_seq.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#define OP(s, state, x) namespace x {
#define NS(...) BOOST_PP_SEQ_FOR_EACH(OP, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
NS(foo, bar, baz)
这扩展为
namespace foo { namespace bar { namespace baz {
这可以做得更简单:
#define OP(s, state, x) state namespace x {
#define NS(...) BOOST_PP_SEQ_FOLD_LEFT(OP, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
您不必单独处理第一个命名空间,这样您就不必在 NS
宏本身中编写 namespace
。
本题基于
- C++ namespaces advice 和
我想效仿
namespace foo::bar::baz {
在 C++17 到来之前使用宏。
我的思路是:
#define BOOST_PP_VARIADICS
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq/fold_left.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>
#define OP(s, state, x) BOOST_PP_CAT(state, BOOST_PP_CAT( { namespace, x )) {
#define NS(...) namespace BOOST_PP_SEQ_FOLD_LEFT(OP, BOOST_PP_SEQ_HEAD(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)), BOOST_PP_SEQ_TAIL(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)))
NS(foo, bar, baz)
基于第二个 link,但这给了我:
namespace foo { namespacebar { namespacebaz {
如何在 namespace
和标识符之间添加 space?
编辑:
如果您可以制作一个宏,使 ns(foo::bar::baz)
扩展为 namespace foo { namespace bar { namespace baz {
,那就更好了。
你可以用 BOOST_PP_SEQ_FOR_EACH
做的更简单:
#define BOOST_PP_VARIADICS
#include <boost/preprocessor/variadic/to_seq.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#define OP(s, state, x) namespace x {
#define NS(...) BOOST_PP_SEQ_FOR_EACH(OP, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
NS(foo, bar, baz)
这扩展为
namespace foo { namespace bar { namespace baz {
这可以做得更简单:
#define OP(s, state, x) state namespace x {
#define NS(...) BOOST_PP_SEQ_FOLD_LEFT(OP, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
您不必单独处理第一个命名空间,这样您就不必在 NS
宏本身中编写 namespace
。