使用声明不能在函数范围内重复。这是为什么? [英] A using-declaration can not be repeated in function scope. Why is that?

查看:180
本文介绍了使用声明不能在函数范围内重复。这是为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在[namespace.udecl] / 10中,您有以下示例:

In [namespace.udecl]/10 you have the following example:

namespace A {
    int i;
}
namespace A1 {
    using A::i;
    using A::i; // OK: double declaration
}
void f() {
    using A::i;
    using A::i; // error: double declaration
}

这段代码在clang中编译。

This snippet compiles in clang.

推荐答案

第一个是在命名空间内的声明,多个using语句可能会频繁使用#includes。第二个是在一个函数的定义内,你永远不会这样做,除非你犯了一个错误。例如,你不能定义相同的符号两次,但你可以声明几次。

The first is a declaration inside a namespace, and the multiple using statements could happen frequently using #includes. The second is inside a definition of a function, and you would never do that unless you made a mistake. You can't define the same symbol twice either, for example, but you can declare several times.

using语句也不仅仅是一个声明。它有点强大,因为它从一个命名空间导入一个函数到另一个。例如,它可以将受保护的基类成员函数拉入派生类中,使其成为公共的。这几乎是一个链接的定义。

The using statement is also more than just a declaration. It is a bit stronger, as it imports a function from one namespace to another. For example, it can pull a protected base class member function into a derived class, making it public. It's almost a definition by linkage.

这篇关于使用声明不能在函数范围内重复。这是为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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