真实世界中命名空间别名的优点vs定义

编程入门 行业动态 更新时间:2024-10-23 17:28:47
本文介绍了真实世界中命名空间别名的优点vs定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

编辑:我打算重构一些代码,并用命名空间别名替换 define 。我不能这样做,只是因为宏是邪恶的。我需要解释为什么我要进行更改, 如果我不这样做可能会出错。

I'm planing to refactor some code, and replace the define with a namespace alias. I can't do this though just because "macros are evil". I need to explain why I want to make the change and what can go wrong if I don't.

抛开macros is evil的立场,在命名空间别名上的 #define ?

Leaving aside the stance that "macros are evil", what are the downfalls of #define over a namespace alias?

取代码

#define MY_NAMESPACE my_namespace

namespace MY_NAMESPACE = my_namespace;

具有别名的原因不在问题的范围内。你还可以假定命名空间的名称是唯一的,它不会出现在任何其他地方(即它只是引用该命名空间,它不能 - 不是现在,不是在未来 - 引用一个变量或类,

The reason for having aliases is not in the scope of the question. You can also assume the name of the namespace is unique enough that it doesn't appear anywhere else (i.e. it just refers to that namespace, it can't - not now, not in the future - refer to a variable or a class or whatever), so there can be no ambiguity there.

推荐答案

在这种特殊情况下,这取决于。如果使用命名空间别名做的伎俩,一切意味着更喜欢它的宏,所有的通常的原因。但两者做了完全不同的事情。您无法使用其别名打开命名空间,即:

In this particular case, it depends. If using a namespace alias does the trick, by all means prefer it to macros, for all of the usual reasons. But the two do radically different things. You cannot open a namespace using its alias, i.e.:

namespace XYZ_ver1 {} namespace XYZ = XYZ_ver1; namespace XYZ { // Illegal! }

事实上,你可以在命名空间出现之前定义宏。如果你需要这个,那么你需要使用一个宏。

This works with a macro; in fact, you can define the macro before the namespace has ever appeared. If you need this, then you need to use a macro.

更多推荐

真实世界中命名空间别名的优点vs定义

本文发布于:2023-11-09 05:59:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571573.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:别名   优点   定义   真实   世界

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!