Added sections about extended friend declarations

git-svn-id: https://cppannotations.svn.sourceforge.net/svnroot/cppannotations/trunk@620 f6dd340e-d3f9-0310-b409-bdd246841980
This commit is contained in:
Frank B. Brokken 2012-01-15 13:04:40 +00:00
parent 893561e1f9
commit 161afb48f9
4 changed files with 68 additions and 1 deletions

View file

@ -101,6 +101,9 @@ includefile(classtemplates/friends)
subsect(Unbound templates as friends)
includefile(classtemplates/unboundfriends)
subsect(Extended friend declarations (C++11, 4.7))
includefile(classtemplates/extended)
lsect(DERIVEDTEMPCLASS)(Class template derivation)
includefile(classtemplates/derived)

View file

@ -0,0 +1,35 @@
The C++11 standard defines emi(extended friend) em(declarations). Extended
friend declarations are also available for class templates.
Extended friend declarations for class templates allow us to use template type
parameters as friend declarations. A template type argument, however, does not
necessarily have to be a type for which the keyword tt(friend) makes sense,
like tt(int). In those cases the friend declaration is simply ignored.
Consider the following class template, declaring tt(Friend) as a friend:
verb(
template <typename Friend>
class Class
{
friend Friend;
void msg(); // private, displays some message
};
)
Now, an actual tt(Friend) class may access all of tt(Class)'s members
verb(
class Concrete
{
Class<Concrete> d_class;
Class<std::string> d_string;
public:
void msg()
{
d_class.msg(); // OK: calls private Class<Concrete>::msg()
//d_string.msg(); // fails to compile: msg() is private
}
};
)
A declaration like tt(Class<int> intClass) is also OK, but here the friend
declaration is simply ignored. After all, there are no `int members' to access
tt(Class<int>)'s private members.

View file

@ -5,5 +5,5 @@ includefile(friends/intro)
lsect(FriendsFriendfun)(Friend functions)
includefile(friends/friendfun)
sect(Extended friend declarations (C++11, 4.7)) TODO
sect(Extended friend declarations (C++11, 4.7))
includefile(friends/extended)

29
yo/friends/extended.yo Normal file
View file

@ -0,0 +1,29 @@
C++11 simplifies tt(friend) declarations by adding
emi(extended friend) em(declarations)
hi(friend: extended declaration)
to the language. When a class is declared as a friend, then the
tt(class) keyword no longer has to be provided. E.g.,
verb(
class Friend; // declare a class
typedef Friend FriendType; // and a typedef for it
using FName = Friend; // and a using declaration
class Class1
{
friend Friend; // FriendType and FNaem: also OK
};
)
In the pre-C++11 standards the friend declaration required an explicit
tt(class); e.g., tt(friend class Friend).
The explicit use of tt(class) remains required if the compiler hasn't seen
the friend's name yet. E.g.,
verb(
class Class1
{
// friend Unseen; // fails to compile: Unseen unknown.
friend class Unseen; // OK
};
Section ref(TEMPFRIENDS) covers the use of extended friend declarations in
class templates.