加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何根据别名对名称空间进行别名并扩展原始名称空间

发布时间:2020-12-16 10:06:22 所属栏目:百科 来源:网络整理
导读:我在C中遇到了当前的问题: 我有一个给定外部库的命名空间.我不喜欢这个外部命名空间的名称,所以我做了以下这样的事情: namespace mynamespace = othernamespace; 如果我想说以下内容,这可以正常工作: mynamespace::foo(...); 但是我发现我需要一个特殊的
我在C中遇到了当前的问题:

我有一个给定外部库的命名空间.我不喜欢这个外部命名空间的名称,所以我做了以下这样的事情:

namespace mynamespace = othernamespace;

如果我想说以下内容,这可以正常工作:

mynamespace::foo(...);

但是我发现我需要一个特殊的函数来将othernamespace中的值转换为外部库中的值.我通过以下方式定义了一个函数:

Y xToY(mynamespace::X x);

我意识到我只想在mynamespace的上下文中使用这个函数,我仍然不喜欢使用其他名称空间的命名空间名称所以我认为很简单,我会做以下事情:

namespace mynamespace = othernamespace;

namespace mynamespace{
    Y xToY(mynamespace::X x);
}

但是我得到以下编译器错误告诉我这不能做到:

myheader.h:13:11: error: namespace alias 'mynamespace' not allowed here,assuming 'othernamespace'

因此它不会编译.注意我目前正在使用c 14.我想知道是否可以使用命名空间othernamespace的别名来扩展此命名空间.

解决方法

不.不幸的是,这是不可能的.来自[namespace.def]的规则明确排除了别名:

In a named-namespace-definition,the identifier is the name of the namespace. If the identifier,when looked up (3.4.1),refers to a namespace-name (but not a namespace-alias) that was introduced in the
namespace in which the named-namespace-definition appears or that was introduced in a member of the inline namespace set of that namespace,the namespace-definition extends the previously-declared namespace.
Otherwise,the identifier is introduced as a namespace-name into the declarative region in which the named-namespace-definition appears.

您不能通过别名扩展名称空间,只能通过原始名称空间名称扩展名称空间.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读