将类型添加到 std 命名空间

发布于 2024-07-09 14:20:02 字数 219 浏览 3 评论 0原文

将类型添加到 std 命名空间是否可以接受。 例如,我想要一个 TCHAR 友好的字符串,那么以下可以接受吗?

#include <string>

namespace std
{
    typedef basic_string<TCHAR> tstring;
}

或者我应该使用自己的命名空间?

Is it acceptable to add types to the std namespace. For example, I want a TCHAR-friendly string, so is the following acceptable?

#include <string>

namespace std
{
    typedef basic_string<TCHAR> tstring;
}

Or should I use my own namespace?

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

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

发布评论

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

评论(7

倾城泪 2024-07-16 14:20:02

仅允许专业化。 例如,您可以为您的类型专门化 std::numeric_limits 。 当然,这必须发生在命名空间 std:: 中。 但您的 typedef 不是专门化的,因此会导致未定义的行为。

Only specializations are allowed. So for example, you are allowed to specialize std::numeric_limits for your type. And this of course must happen in namespace std::. But your typedef isn't a specialization so that's causing undefined behavior.

可爱咩 2024-07-16 14:20:02

不...命名空间的一部分目的是防止升级时发生名称冲突。

如果您将内容添加到 std 命名空间,那么如果他们决定添加同名的内容,您的代码可能会随着库的下一个版本而中断。

No ... part of the point of a namespace is to prevent name collisions on upgrade.

If you add things to the std namespace, then your code might break with the next release of the library if they decide to add something with the same name.

笔落惊风雨 2024-07-16 14:20:02

[C++11: 17.6.4.2.1/1]: 如果 C++ 程序将声明或定义添加到命名空间 std 或除非另有说明,否则到命名空间 std 内的命名空间。 仅当声明依赖于用户定义类型并且专门化满足原始模板的标准库要求并且未明确禁止时,程序才可以将任何标准库模板的模板专门化添加到命名空间 std。

[C++11: 17.6.4.2.1/1]: The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

几度春秋 2024-07-16 14:20:02

您应该使用自己的命名空间,因为向标准库添加代码只会使在线查找有关添加信息的用户感到困惑。

std 中的所有内容都应该只是标准库,而没有其他内容。

You should use your own namespace as adding code to the standard library will only confuse the users that will look online for informations about that addition.

All that is in std should be only the standard library and nothing else.

小梨窩很甜 2024-07-16 14:20:02

该标准正式称这是“未定义的行为”,并且可能会发生各种令人讨厌的事情。

在实践中,它会很好地工作,但你仍然不应该这样做。 除了让人们误以为编译器提供了某些东西之外,它还能给你带来什么?

Officially, the standard says that's "undefined behaviour", and all kinds of nasty things can happen.

In practice, it will work fine, but you still shouldn't do it. What does it buy you, other than confusing people that something is provided by the compiler?

知你几分 2024-07-16 14:20:02

我完全同意其他答案,即您应该将类​​型放在自己的名称空间中,以避免不幸的名称冲突。

但是,我想准确地说,有时,您可以(并且应该!)在 std 命名空间中添加内容。 例如,std::swap 方法的模板特化就是这种情况,它用于提供交换对象的统一方法。 有关此问题的更多信息,您可以阅读非抛出交换习语

I totally agree with other answers saying that you should put your types in your own namespace to avoid unfortunate name collisions.

However, I wanted to precise that sometimes, you can (and should !) add stuff in the std namespace. This is the case for template specializations of the std::swap method for example, which are used to provide a uniform way to swap objects. For more information on this matter, you can read about the non-throwing swap idiom.

溇涏 2024-07-16 14:20:02

这是一个有趣的问题,因为它完全取决于项目和工程师接受的编码标准。

对于单个程序员来说,为什么不......小心一点。

对于团队来说,制定一个标准......

对于跨平台项目,是的。

否则,诺达格。

This is an interesting question because it's completely subjective to the project and the engineers' accepted coding standards.

For a single programmer, why not... just be careful.

For teams, make a standard...

For a cross-platform project, hell yeah.

Otherwise, nawdawg.

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