Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compiler error with friend

Reply
Thread Tools

Compiler error with friend

 
 
James Kanze
Guest
Posts: n/a
 
      01-14-2011
On Jan 14, 1:20 pm, Leigh Johnston <(E-Mail Removed)> wrote:
> On 14/01/2011 13:03, Leigh Johnston wrote:


[...]
> > I apologize;


Accepted.

[...]
> Actually my correction was wrong; instead we were both initially
> correct; *injection* is happening but the function is only accessible
> via ADL:


> struct foo
> {
> public:
> friend void friend_name_injection() { std::cout << "friend name
> injection"; }
> public:
> foo() : iPrivateBits(42) {}
> private:
> int iPrivateBits;


> };


> void friend_name_injection() // error, function already has a body.
> {
> }


That's not injection, at least not in the sense used for friend
name injection in the ARM, nor for class name injection in the
current standard. But you're right that strange things are
happening. The friend function is defined in the namespace,
even if its name has not been injected (and is not visible).
You can'd define it again, because of the one definition rule;
it does exist, even if its name is not visible.

--
James Kanze
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      01-15-2011
On Jan 14, 7:56 pm, Leigh Johnston <(E-Mail Removed)> wrote:
> On 14/01/2011 18:44, James Kanze wrote:
> > On Jan 14, 1:20 pm, Leigh Johnston<(E-Mail Removed)> wrote:
> >> On 14/01/2011 13:03, Leigh Johnston wrote:


> > [...]
> > That's not injection, at least not in the sense used for friend
> > name injection in the ARM, nor for class name injection in the
> > current standard. But you're right that strange things are
> > happening. The friend function is defined in the namespace,
> > even if its name has not been injected (and is not visible).
> > You can'd define it again, because of the one definition rule;
> > it does exist, even if its name is not visible.


> So what term would you use for this if not "friend name injection"?
> "Friend definition" as opposed to "friend declaration" perhaps?


That's a good question. I don't know. The standard doesn't use
any name for it; it just says that the name will be found in
this context, and not in some other context. In the discussions
in the standards committee, name injection means that the name
will be found in all contexts in which name lookup includes that
scope. In this case, we have:

-- The name will be found in ADL, but not in other name
lookups. (In ADL, it will be found "as if" name injection
had occured.)

-- The name is defined in the given scope, even if it isn't
visible. I don't think this is a real problem (unlike the
issue of ADL); whether something is defined or not is more
or less independent of visibility. (If I define a function
f() in global scope a.cpp, it isn't visible in other
translation units, but it is still defined in global scope,
and a second definition violates the one definition rule.)

I really don't know how to describe the first case. Conditional
name injection would seem a valid description to me, even if the
standard doesn't talk about name injection.

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments