From: Öö Tiib on
On Apr 28, 10:02 am, Anubhav <rkld...(a)gmail.com> wrote:
> > > int main(){}
>
> > > My query:
>
> > > 1. why is storage specifier not allowed in friend function
> > > declaration?
>
> > There is no use for this, because a static member function
> > has already access to all private members and types of
> > the corresponding class type.
>
> My intention was to declare 'afriend' as a file scope static function
> which is also a friend of 'Sample' all in one go. I did not intend it
> to be a member function.
>
> Why is this prohibited?

Because "static" inside class definition has very different meaning.
If you want to specify that your afriend() is static then you perhaps
define it outside of class:

class Sample{
friend void afriend(Sample& s);
int member;
};

static void afriend(Sample& s) {
s.member = 42;
}

int main(){
Sample x;
afriend(x);
}

Such static (for translation unit local functions) is deprecated and
your original construct would confuse people pointlessly if it was not
prohibited.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Hounsome on
On 28 Apr, 08:02, Anubhav <rkld...(a)gmail.com> wrote:
> > > int main(){}
>
> > > My query:
>
> > > 1. why is storage specifier not allowed in friend function
> > > declaration?
>
> > There is no use for this, because a static member function
> > has already access to all private members and types of
> > the corresponding class type.
>
> My intention was to declare 'afriend' as a file scope static function
> which is also a friend of 'Sample' all in one go. I did not intend it
> to be a member function.
>
> Why is this prohibited?

There is no such thing as file scope because C++ does not recognize
the existence of files but only of compilation units. A .cpp file
might be a compilation unit but a header file, where classes are
usually declared, never is since the compilation unit will include at
the very least the cpp file that includes the header and most likely a
load of standard headers too.

If the class was declared in a header file then, if it meant what you
thought, you would be trying to refer to a different friend in each
compilation unit in which it was included.

There is really no point that I can think of in trying to do what you
are trying to do.

If you really want friends that are not global they can be declared in
a namespace.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
Anubhav wrote:
>>> int main(){}
>>> My query:
>>> 1. why is storage specifier not allowed in friend function
>>> declaration?
>> There is no use for this, because a static member function
>> has already access to all private members and types of
>> the corresponding class type.
>
> My intention was to declare 'afriend' as a file scope static function
> which is also a friend of 'Sample' all in one go. I did not intend it
> to be a member function.
>
> Why is this prohibited?
>

Because it makes no sense. It would mean that you had a separate version of the function in every file in which you included the header with the class definition, why would you want to do that?

They language problem is that static in the context of a class has a different meaning to static used outside a class definition and by using friend you have left the compiler with an irresolvable conflict of meanings.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 28 Apr., 21:15, �� Tiib <oot...(a)hot.ee> wrote:
> On Apr 28, 10:02 am, Anubhav <rkld...(a)gmail.com> wrote:
>
> > > > int main(){}
>
> > > > My query:
>
> > > > 1. why is storage specifier not allowed in friend function
> > > > declaration?
>
> > > There is no use for this, because a static member function
> > > has already access to all private members and types of
> > > the corresponding class type.
>
> > My intention was to declare 'afriend' as a file scope static function
> > which is also a friend of 'Sample' all in one go. I did not intend it
> > to be a member function.
>
> > Why is this prohibited?
>
> Because "static" inside class definition has very different meaning.
> If you want to specify that your afriend() is static then you perhaps
> define it outside of class:
>
> class Sample{
> friend void afriend(Sample& s);
> int member;
> };
>
> static void afriend(Sample& s) {
> s.member = 42;
> }
>
> int main(){
> Sample x;
> afriend(x);
> }

The preceding declaration of afriend with the correct
linkage specification is required, see [class.friend]/3:

"A function first declared in a friend declaration has
external linkage (3.5). Otherwise, the function retains
its previous linkage (7.1.1)."

Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
Daniel Krügler wrote:
> static void afriend();
>
> struct Sample {
> private:
> int m;
> friend void afriend();
> } s;
>
> static void afriend() {
> s.m = 12;
> }

Just wondering, is this an ODR violation when included more than once? The
point is that different translation units have different afriend()s and
class Sample thus picks a different afriend() every time.


Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]