From: Anubhav on 26 Apr 2010 23:01 class Sample{ static friend void afriend(){} }; int main(){} My query: 1. why is storage specifier not allowed in friend function declaration? 2. Which part of the standard actually mentions about this restriction? Regards, Dabs -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 27 Apr 2010 07:23 Anubhav wrote: > class Sample{ > static friend void afriend(){} > }; [...] > 1. why is storage specifier not allowed in friend function > declaration? I wouldn't call it a "storage specifier". Rather, the "static" when applied to a memberfunction of a class signals that this function is not a bound memberfunction (one which always receives the implicit "this" parameter) but a normal function nested in the class. As such, the full name of above function would be "Sample::afriend". A friend function, even if defined inline, always refers to a function that is outside the class. In this case, it's a global function who's full name would be "afriend". I hope that explains why those don't mix. In case you were trying to solve a problem, I have to tell you there is none. Class-static functions always get full access to privates just like friend functions or bound memberfunctions. Further, friend functions never receive the implicit "this" parameter, so making them static doesn't anything anyway. Uli -- [ 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 27 Apr 2010 07:22 On 27 Apr., 16:01, Anubhav <rkld...(a)gmail.com> wrote: > class Sample{ > static friend void afriend(){} > }; > > 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. Any defining friend-declaration refers to a function in namespace scope, see [class.friend]/5. > 2. Which part of the standard actually mentions about this > restriction? See [class.friend]/6: "No storage-class-specifier shall appear in the decl-specifier-seq of a friend declaration." HTH & 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: Anubhav on 27 Apr 2010 16:02 > > 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? -- [ 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 28 Apr 2010 04:14 On 28 Apr., 09: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. This did not became clear from your original posting. > Why is this prohibited? This is not prohibited, but you need to change the way of realizing this: static void afriend(); struct Sample { private: int m; friend void afriend(); } s; static void afriend() { s.m = 12; } int main() { afriend(); } The way you tried it in your OP does declare a function with external linkage, 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)." The above shown way ensures that we declare afriend first as a function with internal linkage, which is later declared as a friend. HTH & 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! ]
|
Next
|
Last
Pages: 1 2 3 Prev: Static const size_t member initialization Next: cv qualified return types |