Reputation: 131829
After searching aroung SO, one question taught me that the lexical scope of an inline friend function is the class it's defined in, meaning it can access e.g. the typedef
s in the class without qualifying them. But then I wondered what is the actual scope of such a function? GCC at least rejects all my attempts to call it. Can a function such as in the example ever be called through means other than ADL, which is not possible here thanks to no arguments?
Standard quotations are appreciated, as I currently can't access my copy of it.
namespace foo{
struct bar{
friend void baz(){}
void call_friend();
};
}
int main(){
foo::baz(); // can't access through enclosing scope of the class
foo::bar::baz(); // can't access through class scope
}
namespace foo{
void bar::call_friend(){
baz(); // can't access through member function
}
}
results in these errors:
prog.cpp: In function ‘int main()’:
prog.cpp:9: error: ‘baz’ is not a member of ‘foo’
prog.cpp:10: error: ‘baz’ is not a member of ‘foo::bar’
prog.cpp: In member function ‘void foo::bar::call_friend()’:
prog.cpp:15: error: ‘baz’ was not declared in this scope
Upvotes: 38
Views: 7946
Reputation: 41
I ran into a similar problem... The non-intuitive non-functioning of the code below really surprised me, although it's probably within the standard :-(
#include <iostream>
#include <cassert>
/// Alternative "fill" for `u_type`
struct opaque {
unsigned val;
constexpr opaque(): val(0xffffffff){}
constexpr opaque(int v) : val(v) {}
constexpr opaque(const opaque& other) : val(other.val) {}
operator int() const { return val; }
};
class unique_test {
public:
using u_type = opaque; //!< Type which has to be "uniqued".
//using u_type = double; //!< Other alternative. Not works same way.
static constexpr u_type ONE=10;
friend std::ostream& operator << (std::ostream& os, const u_type* dummy) { return os<<"u_type"; } //OK
friend std::ostream& operator << (std::ostream& os, const unique_test& u) { return os<<"unique_test"; } //OK
friend const u_type* check10(const unique_test& dummy) { return &ONE; } //OK
friend const u_type* check10(const unique_test* dummy) { return &ONE; } //OK
friend const u_type* check10(const u_type& dummy) { return &ONE; } //NOT VISIBLE BELOW!!!
friend const u_type* check10(const u_type* dummy) { return &ONE; } //NOT VISIBLE BELOW!!!
friend const u_type* check10(const unsigned* dummy) { return &ONE; } //NOT VISIBLE BELOW!!!
friend const u_type* check10(const unsigned dummy) { return &ONE; } //NOT VISIBLE BELOW!!!
unique_test() {
u_type dum;
std::cout<<&dum<<' '<<*this<<std::endl;
const u_type* existed = check10( this ); assert(existed!=nullptr);
}
void test() {
u_type dum;
std::cout<<&dum<<' '<<*this<<std::endl;
const u_type* existed = check10( *this ); assert(existed!=nullptr);
}
};
class unique_test_der : public unique_test {
public:
static constexpr u_type TWO=11;
friend const u_type* check11(const unique_test* dummy) { return &TWO; } //VISIBLE INSIDE BUT NOT OUTSIDE!
friend const u_type* check11(const unique_test& dummy) { return &TWO; } //VISIBLE INSIDE BUT NOT OUTSIDE!
unique_test_der() {
u_type dum;
std::cout<<&dum<<' '<<*this<<std::endl;
const u_type* existed = check11( this ); assert(existed!=nullptr);
existed = check11( *this ); assert(existed!=nullptr);
}
void test() {
u_type dum;
std::cout<<&dum<<' '<<*this<<std::endl;
const u_type* existed = check11( this ); assert(existed!=nullptr);
existed = check11( *this ); assert(existed!=nullptr);
}
};
void test_uniq_test(unique_test& dumdum) {
unsigned dummie=1;
unique_test::u_type dum;
std::cout<<&dum<<' '<<dumdum<<std::endl;
const unique_test::u_type* existed10 = check10( &dumdum ); assert(existed10!=nullptr);
existed10 = check10( dumdum ); assert(existed10!=nullptr);
const unique_test::u_type* existed11 = check11( &dumdum ); // error: ‘check10’ was not declared in this scope
const unique_test::u_type* existed2 = check10( &dum ); // error: ‘check10’ was not declared in this scope
const unique_test::u_type* existed3 = check10( dum ) // error: ‘check10’ was not declared in this scope
const unique_test::u_type* existed4 = check10( &dummie); // error: ‘check10’ was not declared in this scope
const unique_test::u_type* existed5 = check10( dummie ); // error: ‘check10’ was not declared in this scope
}
Upvotes: 0
Reputation: 11046
A friend
function defined within an enclosing class, will only be found by argument dependent lookup (ADL). Calling it will succeed when one or more of the arguments is either of the enclosing class type; or of a type declared within the class. Here is an example, displaying "Hello World!", which (for variety) doesn't use an object of the enclosing class type to provide such an argument:
#include <iostream>
struct foo
{
struct local_class{};
friend void greet(local_class o, int) { std::cout << "Hello World!\n"; }
};
int main(int argc, char *argv[])
{
foo::local_class o;
greet(o,1);
return 0;
}
Upvotes: 1
Reputation:
"The C++ Programming Language 3rd Edition (Stroustrap)" : p279:
I. "Like a member declaration, a friend declaration does not introduce a name into an enclosing scope"
II. "A friend class must be previously declared in an enclosing scope or defined in the nonclass scope immediately enclosing the class that is declaring it a friend"
III. "A friend function can be explicitly declared just like friend classes, or it can be found through its argument types (§8.2.6) as if it was declared in the nonclass scope immediately enclosing its class."
IV. "It follows that a friend function should either be explicitly declared in an enclosing scope or take an argument of its class. If not, the friend cannot be called. For example:"
//no f() here
void g();
class X{
friend void f(); //useless
friend void g(); //can be found because it is declared outside of class scope
friend void h(const X&); //can be found because the arguments access class members
};
void f() { } //enemy of X :)
But in your case there is more to it that has to do with the namespace, because if you put the proper declaration in foo e.g.:
namespace foo{
struct bar{
friend void baz(const &bar){};
void call_friend();
}
}
does not compile. However, if you declare it outside foo is works like a charm. Now consider that in fact, global, local, struct, and classes are in fact namespaces. Now this leads to the conclusion that the baz(const &)
is implicitly defined in the global scope.
This compiles:
namespace foo{
struct bar{
friend void baz(const bar&){};
void call_friend();
};
}
int main(){
foo::bar k;
baz(k);
return 0;
}
Therefore, there are two issues:
Thanks, hope it helps, but certainly, I liked the challenge :) .
Upvotes: 6
Reputation: 792527
When you declare a friend
function with an unqualified id in a class it names a function in the nearest enclosing namespace scope.
If that function hasn't previously been declared then the friend
declaration doesn't make that function visible in that scope for normal lookup. It does make the declared function visible to argument-dependent lookup.
This is emphasised in many notes, but the definitive statement is in 7.3.1.2/3 (of ISO/IEC 14882:2011):
Every name first declared in a namespace is a member of that namespace. If a
friend
declaration in a non-local class first declares a class or function the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by unqualified lookup (3.4.1) or by qualified lookup (3.4.3) until a matching declaration is provided in that namespace scope (either before or after the class definition granting friendship). If a friend function is called, its name may be found by the name lookup that considers functions from namespaces and classes associated with the types of the function arguments (3.4.2). If the name in afriend
declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.
Upvotes: 42
Reputation: 17577
In this Example,
namespace foo{
struct bar{
friend void baz(){}
void call_friend();
};
}
int main(){
foo::baz(); // can't access through enclosing scope of the class
foo::bar::baz(); // can't access through class scope
}
namespace foo{
void bar::call_friend(){
baz(); // can't access through member function
}
}
foo::baz()
is inaccessible because the name baz
is not visible in scope of namespace foo
. If I remember correctly (§ 3.4/2) applies here.
foo::bar::baz()
is inaccessible because friends aren't members of class and the scope of inline friend function is namespace or class in which their definition exists therefore, you can't access them outside of that scope.
If you put the declaration of baz()
in foo
the name of function baz
will be visible in foo
and the definition will be looked up in the nested scope of bar
.
namespace foo{
void baz(); // declaration at namespace scope
struct bar{
friend void baz(){}
};
void call_friend() {
baz(); // ok
}
}
int main()
{
foo::baz(); // ok, bar will be looked up in the nested scope of foo::bar.
}
Upvotes: 0
Reputation: 264571
Interesting!
It seems that the compiler does not know what scope it belongs to (and to be honest there are no clues) and thus puts in in no scope. Some standard digging coming up I suppose.
Note: If you explicitly add a declaration to a particular scope then it starts to work as expected.
namespace foo
{
void baz(); // declare it here and now it works in foo namespace etc.
struct bar
{
friend void baz(){}
void call_friend();
};
}
Digging the standard I find:
A function can be defined in a friend declaration of a class if and only if the class is a non-local class (9.8), the function name is unqualified, and the function has namespace scope.
[ Example:
class M { friend void f() { } // definition of global f, a friend of M,
// not the definition of a member function
};
— end example ]
Such a function is implicitly inline. A friend function defined in a class is in the (lexical) scope of the class in which it is defined. A friend function defined outside the class is not (3.4.1).
A free standing function that does not take a parameter is not much use as a friend. As it will have no object on which to take advantage of its friendship (I suppose file scope static storage duration objects).
Upvotes: 2
Reputation: 27258
I think you are confusing friend
and private
. By declaring function a friend
you are granting it access to your private members, and not grating other functions access to it. Anyway, any member function of a struct
is accessible by any object because struct
members are public by default.
However, in your case baz
isn't accessible because by doing friend void baz(){}
you didn't really declare the function baz
, you just said that it is a friend
function. You can just remove the friend
keyword, and it will solve all the issues.
Upvotes: -2