C ++标准第3.4.3.1节中此声明的示例?

时间:2015-08-05 21:28:21

标签: c++

有人可以发布一个有用的例子来说明(1.4)。

  

如果qualified-id的嵌套名称说明符指定了一个类,则在类(10.2)的范围内查找嵌套名称指定符后指定的名称,但下面列出的情况除外。该名称应代表该类别或其基类之一的一个或多个成员(第10条)。 [注意:可以在潜在范围内的任何一点使用qualified-id引用类成员(3.3.7)。 - 尾注]以上名称查找规则的例外情况如下:

     

(1.1) - 按照

中的指定查找析构函数名称      

(1.2) - 以类成员中的conversion-type-id相同的方式查找conversion-function-id的conversion-type-id   访问(见3.4.5);

     

(1.3) - 在发生整个后缀表达式的上下文中查找template-id的template-argument中的名称。

     

(1.4) - 查找using-declaration(7.3.3)中指定的名称也会发现隐藏在同一范围内的类或枚举名称(3.3.10)。

2 个答案:

答案 0 :(得分:1)

这是CWG 400(引用N0905)的解决方案,如果以下代码格式正确,则会提出疑问:

struct A { int i; struct i {}; };
struct B { int i; struct i {}; };
struct D : public A, public B { using A::i; void f (); };
void D::f () { struct i x; }

标准中添加了要点,以明确using A::i struct i { } int i带入 Dstruct i x格式正确。

答案 1 :(得分:0)

我没有查看报价,但我认为这个例子是相关的

#include <iostream>

namespace N1
{    
struct A
{
    static int x;
};
}    

using N1::A;

int x = 10;
int N1::A::x = 20;

int A = 30;

int main()
{

    std::cout << A::x << std::endl;
}    

程序输出

20

同一范围内的字样不明确。所以你可以考虑一个修改过的例子

#include <iostream>

namespace N1
{    
struct A
{
    static int x;
};

int A = 30;
}    

using N1::A;

int x = 10;
int N1::A::x = 20;

int main()
{

    std::cout << A::x << std::endl;
}    

在两个程序中声明对象A的类型为int

int A = 30;

隐藏struct A的声明。