()
, , ::, , , , , . (: , , , ::, , , , , , )
, using, , , , , using, .
, (, , ), , .
, ( ), - , , :
int n = 1; // n
int x = n + 1; // OK: ::n
int z = y - 1; // :
int y = 2; // y
, - , , , , .., .
int n = 1; //
namespace N
{
int m = 2;
namespace Y
{
int x = n; // OK, ::n
int y = m; // OK, ::N::m
int z = k; // :
}
int k = 3;
}
, - , , , :
namespace X
{
extern int x; // ,
int n = 1; //
};
int n = 2; // .
int X::x = n; // X::n, X::x 1
,
, , , , , , , , .., , . , , ( ) , , ..
namespace A
{
namespace N
{
void f();
int i=3; // ( )
}
int i=4; // ( )
}
int i=5; // ( )
void A::N::f()
{
int i = 2; // ( )
while(true)
{
int i = 1; // :
std::cout << i;
}
}
// int i; //
namespace A
{
namespace N
{
// int i; //
}
}
friend, , , , , , , .
namespace M
{
// const int i = 1; //
class B
{
// static const int i = 3; // (
// )
};
}
// const int i = 5; //
namespace N
{
// const int i = 4; //
class Y : public M::B
{
// static const int i = 2; //
class X
{
// static const int i = 1; //
int a[i]; // i
// static const int i = 1; //
};
// static const int i = 2; //
};
// const int i = 4; //
}
// const int i = 5; //
-
class B
{
// int i; //
};
namespace M
{
// int i; //
namespace N
{
// int i; //
class X : public B
{
// int i; //
void f();
// int i; //
};
// int i; //
}
}
// int i; //
void M::N::X::f()
{
// int i; //
i = 16;
// int i; //
}
namespace M
{
namespace N
{
// int i; //
}
}
, B, A, A B. ( , - , , A , B: .) , using-, , . , |
( C++11) |
, , . using , , , , , . C , , C. C , Bi ( , Bi ). , C
|
( C++11) |
struct X { void f(); };
struct B1: virtual X { void f(); };
struct B2: virtual X {};
struct D : B1, B2
{
void foo()
{
X::f(); // OK, X::f ( )
f(); // OK, B1::f ( )
// C++98: B1::f X::f, X::f D
// B2, D.
// C++11: f D ,
// f B1, B1::f,
// , f C B1::f B1
// f B2 ,
// f X X::f
// , f B2 X::f X
// C , (X) B2
// (B1), , B2
//
// C B1::f, B1
// ( D : B2, B1, * * C,
// X::f X, , C
// ( X), (B1),
// : C B1::f,
// B1)
}
};
- ,
B,B,B, ,B:
struct V { int v; };
struct A
{
int a;
static int s;
enum { e };
};
struct B : A, virtual V { };
struct C : A, virtual V { };
struct D : B, C { };
void f(D& pd)
{
++pd.v; // OK: v,
++pd.s; // OK: A::s, B C
int i = pd.e; // OK: A::e,
// B C
++pd.a; // , : A::a B A::a C
}
int i = 3; // f1, f2
struct X
{
static const int i = 2; // f1, f2
friend void f1(int x)
{
// int i; //
i = x; // X::i
}
friend int f2();
// static const int i = 2; // f1
};
void f2(int x)
{
// int i; //
i = x; // ::i
}
// , -
struct A
{
typedef int AT;
void f1(AT);
void f2(float);
template <class T> void f3();
};
// ,
struct B
{
typedef char AT;
typedef float BT;
friend void A::f1(AT); // AT A::AT
friend void A::f2(BT); // BT B::BT
friend void A::f3<AT>(); // AT B::AT
};
class X
{
int a, b, i, j;
public:
const int& r;
X(int i): r(a), // X::r X::a
b(i), // X::b i
i(i), // X::i i
j(this->i) // X::j X::i
{ }
};
int a;
int f(int a, int b = a); // : a a, ::a,
//
struct X
{
static int x;
static const int n = 1; //
};
int n = 2; // .
int X::x = n; // X::n, X::x 1, 2
const int RED = 7;
enum class color
{
RED,
GREEN = RED+2, // RED color::RED, ::RED, GREEN = 2
BLUE = ::RED+4 // ::RED, BLUE = 11
};
catch try
, catch try , , , ( , , , , )
int n = 3; //
int f(int n = 2) //
try
{
int n = -1; //
} catch(...)
{
// int n = 1; //
assert(n == 2); // n f
throw;
}
, (, operator+, a+b), , , operator+(a,b): : , , - ( , ). , . , :
struct A {};
void operator+(A, A); // operator+,
struct B
{
void operator+(B); // operator+,
void f ();
};
A a;
void B::f() // - B
{
operator+(a,a); // : -
// operator+ B
// ,
//
a + a; // OK: B::operator+,
// ::operator+(A,A), ::operator+(A,A)
}
, , . , , , . , , , , ADL ( C++11), , , ADL ( C++11), ( , , ADL). , , ADL , - , , . , , ( , ).
void f(char); // f
template<class T>
void g(T t)
{
f(1); // : ::f(char)
f(T(1)); // :
f(t); // :
// dd++; // :
}
enum E { e };
void f(E); // f
void f(int); // f
double dd;
void h()
{
g(e); // g<E>,
// 'f'
// ::f(char) ( ) ::f(E)
// ( ADL), ::f(E).
// f(char), f(E)
g(32); // g<int>,
// 'f'
// ::f(char),
// ::f(char)
// f(char)
}
typedef double A;
template<class T> class B
{
typedef int A;
};
template<class T> struct X : B<T>
{
A a; // A ::A (double), B<T>::A
};
| : -> . |
- C++23 (ISO/IEC 14882:2023):
- 6.5 [basic.lookup](. 44-45)
- 6.5.2 [class.member.lookup](. 45-47)
- 13.8 [temp.res](. 399-403)
- C++20 (ISO/IEC 14882:2020):
- 6.5 [basic.lookup](. 38-50)
- 11.8 M [class.member.lookup](. 283-285)
- 13.8 [temp.res](. 385-400)
- C++17 (ISO/IEC 14882:2017):
- 6.4 [basic.lookup](. 50-63)
- 13.2 [class.member.lookup](. 259-262)
- 17.6 [temp.res](. 375-378)
- C++14 (ISO/IEC 14882:2014):
- 3.4 [basic.lookup](. 42-56)
- 10.2 [class.member.lookup](. 233-236)
- 14.6 [temp.res](. 346-359)
- C++11 (ISO/IEC 14882:2011):
- 3.4 [basic.lookup]
- 10.2 [class.member.lookup]
- 14.6 [temp.res]
- C++98 (ISO/IEC 14882:1998):
- 3.4 [basic.lookup]
- 10.2 [class.member.lookup]
- 14.6 [temp.res]
C++:
| CWG 490 | C++98 | - - |
|
| CWG 514 | C++98 | , , |
, , |