c++ - There are some details I didn't understand in §7.3.1.2/3 in the C++11 Standard -
§7.3.1.2/3 in c++11 standard (emphasis mine):
every name first declared in namespace member of namespace. if friend declaration in nonlocal class first declares class or function friend class or function member of innermost enclosing namespace. name of friend not found unqualified lookup (3.4.1) or qualified lookup (3.4.3) until matching declaration provided in namespace scope (either before or after class definition granting friendship). if friend function called, name may found name lookup considers functions namespaces , classes associated types of function arguments (3.4.2). if name in friend declaration neither qualified nor template-id , declaration function or elaborated-type-specifier, lookup determine whether entity has been declared shall not consider scopes outside innermost enclosing namespace. [ note: other forms of friend declarations cannot declare new member of innermost enclosing namespace , follow usual lookup rules.
example:
// assume f , g have not yet been defined. void h(int); template <class t> void f2(t); namespace { class x { friend void f(x); // a::f(x) friend class y { friend void g(); // a::g friend friend void h(int); // a::h friend // ::h not considered friend void f2<>(int); // ::f2<>(int) friend }; }; // a::f, a::g , a::h not visible here x x; void g() { f(x); } // definition of a::g void f(x) { /* ... */} // definition of a::f void h(int) { /* ... */ } // definition of a::h // a::f, a::g , a::h visible here , known friends } using a::x; void h() { a::f(x); a::x::f(x); // error: f not member of a::x a::x::y::g(); // error: g not member of a::x::y }
unless i'm missing something, don't understand need words first above. far can see, can't have more 1 declaration of entity in namespace, nor more 1 declaration of friend function in class.
also, relevance of comment "assume f , g have yet not been defined" in example? doesn't matter if these functions declared before definition of namespace a. they'll belong global namespace , they'll have nothing functions declared inside namespace a.
edit:
the fact 1 can have repeated declarations of same function, or declaration , definition of function in namespace, doesn't invalidate observation use of words first in §7.3.1.2/3 not necessary.
edit1
i've found error. comment ::f2<>(int) friend
incorrect. not there no definition of template function f2(t)
in namespace a, more important, declaration template <class t> void f2(t);
must inside a, otherwise function f2<>(int)
not friend of class a::x::y
.
a shorter more concise answer complete 1 vlad:
an entity can declared multiple times, premise wrong. in first sentence first important these 2 valid declarations function f
in namespace n
:
namespace n { void f(); } void n::f() { ... } // definition *also* declaration
at point need first in first sentence apparent, f
member of n
namespace (first declaration), not global namespace.
in case of friend declaration first important different reason, if friend declaration first declaration, name not visible regular lookup:
//[1] class y {}; // type class x { x(y); // allow implicit conversions, // exposition purposes friend x operator+(x, x) {...} // *first* declaration of operator }; // , definition void f() { y a, b; + b; // error, no operator+ takes 2 y x c; c + b; // ok, adl can find }
if friend declaration not first declaration, i.e. if [1] gets replaced previous declaration:
class x; x operator+(x,x);
with rest of code being same, above code compile , call operator+(x,x)
converting a
, b
x
.
the last question had on assume f
, g
have not been defined, believe should read declared, not defined. importance of statement if function have been declared before hand, comment // a::f, a::g , a::h not visible here
becomes false, previous declaration makes functions visible.
Comments
Post a Comment