屏蔽#include在namespace {}块中? [英] shielding #include within namespace { } block?

查看:96
本文介绍了屏蔽#include在namespace {}块中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我知道方法1本质上是无效的,可能会使用方法2,但我正在寻找最好的黑客或更好的解决方案来缓解猖獗,可变的命名空间扩散。



我在一个名称空间中有多个具有不同依赖关系的类或方法定义,并且希望尽可能使用最少的名称空间块或显式scopings,但是将#include指令与尽可能最好的需要的定义分组。我从来没有看到任何迹象表明可以告诉任何预处理器从#include内容中排除命名空间{}作用域,但是我在这里询问是否有类似的东西是可能的:(见底部解释为什么我想要死的东西简单)

  //注意:apple.h等,内容是* NOT *意图在名称空间Foo中! 

//最喜欢这个:
#pragma magic_namespace_backout(1)// FIXME:使用实际现有的指令
namespace Foo {

#包括apple.h
B * A :: blah(B const * x){/ * ... * /}

#includebanana.h
int B :: whatever(C const& var){/ * ... * /}

#includeblueberry.h
void B :: something(){/ * ... * /}

} //名称空间Foo

.. 。

  //通过这个:
#includeapple.h
#includebanana。 h
#includeblueberry.h

namespace Foo {
B * A :: blah(B const * x){/ * ... * /}
int B :: whatever(C const& var){/ * ... * /}
void B :: something(){/ * ... * /}
} / / namespace Foo

...

  //或者这个:
#includeapple.h
namespace Foo {
B * A :: blah(B const * x){ / * ... * /}
} //名称空间Foo

#includebanana.h
namespace Foo {
int B :: whatever(C const& var){/ * ... * /}
} // namespace Foo

#includeblueberry.h
namespace Foo {
void B :: something(){/ * ... * /}
} //名称空间Foo

我真正的问题是,我有一些项目,其中模块可能需要分支,但在同一程序中具有来自分支的共存组件。我有类似FooA等的类,我称之为Foo :: A,希望能够像Foo :: v1_2 :: A那样痛苦地分支,其中某些程序可能需要Foo :: A和Foo :: V1_2 ::一个。我希望Foo或Foo :: v1_2只显示每个文件一次,如果可能的话,作为单个命名空间块。此外,我倾向于在需要它们的文件中的第一个定义的正上方找到#include指令块。什么是我最好的选择,或者我应该做什么而不是劫持命名空间?

解决方案

想想#including as将包含文件的内容复制并粘贴到#include指令的位置。



这意味着,是的,包含文件中的所有内容都将位于命名空间内。 / p>

Edit: I know that method 1 is essentially invalid and will probably use method 2, but I'm looking for the best hack or a better solution to mitigate rampant, mutable namespace proliferation.

I have multiple class or method definitions in one namespace that have different dependencies, and would like to use the fewest namespace blocks or explicit scopings possible but while grouping #include directives with the definitions that require them as best as possible. I've never seen any indication that any preprocessor could be told to exclude namespace {} scoping from #include contents, but I'm here to ask if something similar to this is possible: (see bottom for explanation of why I want something dead simple)

// NOTE: apple.h, etc., contents are *NOT* intended to be in namespace Foo!

// would prefer something most this:
#pragma magic_namespace_backout(1) // FIXME: use actually existing directive
namespace Foo {

#include "apple.h"
B *A::blah(B const *x) { /* ... */ }

#include "banana.h"
int B::whatever(C const &var) { /* ... */ }

#include "blueberry.h"
void B::something() { /* ... */ }

} // namespace Foo

...

// over this:
#include "apple.h"
#include "banana.h"
#include "blueberry.h"

namespace Foo {
B *A::blah(B const *x) { /* ... */ }
int B::whatever(C const &var) { /* ... */ }
void B::something() { /* ... */ }
} // namespace Foo

...

// or over this:
#include "apple.h"
namespace Foo {
B *A::blah(B const *x) { /* ... */ }
} // namespace Foo

#include "banana.h"
namespace Foo {
int B::whatever(C const &var) { /* ... */ }
} // namespace Foo

#include "blueberry.h"
namespace Foo {
void B::something() { /* ... */ }
} // namespace Foo

My real problem is that I have projects where a module may need to be branched but have coexisting components from the branches in the same program. I have classes like FooA, etc., that I've called Foo::A in the hopes being able to branch less painfully as Foo::v1_2::A, where some program may need both a Foo::A and a Foo::v1_2::A. I'd like "Foo" or "Foo::v1_2" to show up only really once per file, as a single namespace block, if possible. Moreover, I tend to prefer to locate blocks of #include directives immediately above the first definition in the file that requires them. What's my best choice, or alternatively, what should I be doing instead of hijacking the namespaces?

解决方案

Just think of #including as copying and pasting the contents of the included file to the position of the #include directive.

That means, yes, everything in the included file will be inside the namespace.

这篇关于屏蔽#include在namespace {}块中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆