在命名空间声明之后声明 using 语句

发布于 2024-10-19 16:18:40 字数 608 浏览 4 评论 0原文

我正在编写一个实用程序库,它由几个“包”组成。每个包中的类都包含在不同的命名空间中。我有一个想法,如何通过在类声明末尾自动声明 using 语句来简化情况(见下文),这将避免程序员在 cpp 文件中执行此操作。

namespace Utility
{
    class String
    {
        // Class Implementation
    };
}

using Utility::String;

我的理解是,如果用户包含头文件 String.h 并且 String 在 Utility 中,那么程序员将想要使用 String.h。显然,如果存在外部类链,其中包含一堆弄脏名称空间的文件,这可能会很糟糕,所以我想如何将其改为#define。

namespace Utility
{
    class String
    {
        // Class Implementation
    };
}

#ifdef AUTO_DECLARE_NAMESPACE
    using Utility::String;
#endif

这样,想要此扩展功能的程序员就可以获得它。

这是个好主意还是有什么我忽略的事情?

I am writing a utility library which is made up of several "Packages". The classes in each package are contained in various namespaces. I have an idea as to how I can simplify the situation by automatically declaring using statements at the end of class declarations (see below), this will avoid having the programmer do it in a cpp file.

namespace Utility
{
    class String
    {
        // Class Implementation
    };
}

using Utility::String;

My understanding is that if the user includes the header String.h and String is in Utility then the programmer will want to use String. Obviously this could be bad if there are outside classes chain including a bunch of files which dirty up the namespace so I thought how about making it a #define instead.

namespace Utility
{
    class String
    {
        // Class Implementation
    };
}

#ifdef AUTO_DECLARE_NAMESPACE
    using Utility::String;
#endif

That way, programmers that want this extended functionality can get it.

Would this a good idea or is there something I'm overlooking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独行侠 2024-10-26 16:18:40

如果您只是要为命名空间中声明的每个名称添加 using 声明,那么使用命名空间就没有意义。

让头文件的用户决定他们想要如何使用头文件。如果有人想使用using声明,就让他直接在.cpp文件中使用;这将使 .cpp 文件中的代码更加清晰,因为名称的来源将很明显。

There is no point in using namespaces if you are just going to add a using declaration for each and every name declared in the namespace.

Let users of your header files decide how they want to use the headers. If someone wants to use a using declaration, let him do it in the .cpp file directly; this will make the code in that .cpp file clearer since it will be apparent where the name originated.

寂寞陪衬 2024-10-26 16:18:40

这看起来充其量是毫无意义的,而充其量是令人烦恼的。

让开发人员决定使用哪些名称空间以及完全限定哪些名称空间有什么问题?

This seems at best pointless, and at worst annoying.

What is wrong with having developers decide which namespaces to use and what to qualify fully?

初心 2024-10-26 16:18:40

老实说,我相信这就是 using namespace 指令的用途。考虑到 using namespace 指令就可以做到这一点,因此您无需添加此预处理器机制。

Honestly, I believe that's what the using namespace directive is for. There's no need for you to add this preprocessor mechanism, considering the using namespace directive does just that.

胡渣熟男 2024-10-26 16:18:40

难道你不能有另一个 .h 文件包含你所有的用途,比如 my_lib_import_names.h 并只是 #include 来获得你想要的东西吗?

您可能会遇到未声明类的问题,但您可能可以通过使用以下内容来绕过它:

#ifdef UTILITY_STRING_H_ 
  using Utility::String;
#endif
..
#ifdef UTILITY_SOMETHING_ELSE_H
   using Utility::SomethingElse;
#endif

..

您觉得怎么样?

这样您就可以在库 .h 中保留“预期”行为,但也可以按照您喜欢的方式进行操作。您还可以保留命名空间相对于类的优势(以必须维护新的 .h 文件为代价)。

Couldn't you have another .h file with all your usings like my_lib_import_names.h and just #include that to get what you want?

You would probably have problem with classes not being declared but you could probably bypass it by using something like:

#ifdef UTILITY_STRING_H_ 
  using Utility::String;
#endif
..
#ifdef UTILITY_SOMETHING_ELSE_H
   using Utility::SomethingElse;
#endif

..

What do you think?

That way you could retain the "expected" behavior in your library .h but also have your the way you like. You also get to keep the benefit of the namespace over your classes (at the expense of having to maintain your new .h file).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文