{"id":6230,"date":"2021-09-29T20:27:12","date_gmt":"2021-09-29T20:27:12","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/special-friendship-of-templates\/"},"modified":"2024-07-22T15:21:12","modified_gmt":"2024-07-22T15:21:12","slug":"special-friendship-of-templates","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/special-friendship-of-templates\/","title":{"rendered":"The Special Friendship of Templates"},"content":{"rendered":"<p>A friend has unrestricted access to the members of a class. Consequently, friendship should be given wisely. Regarding templates, friendship is special.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6208\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/templates.png\" alt=\"templates\" width=\"650\" height=\"401\" \/><\/p>\n<p>Before I write about the friendship rules for templates, I want to present the general rules about friendship.<\/p>\n<ol>\n<li>The friend declaration can be made in any place in the class.<\/li>\n<li>For friendship, access rights in the class are not considered.<\/li>\n<li>Friendship is not inherited. When a class grants friendship to a class Derived, a\u00a0 <code>Derived<\/code> derived class is not automatically a friend to Base.<\/li>\n<li>Friendship is not transitive. When class<code> B<\/code> is a friend of class A and Class<code> C<\/code> is a friend of class<code> B<\/code>, class<code> C<\/code> is not automatically a friend of class<code> A<\/code>.<\/li>\n<\/ol>\n<p>A class or a class template can have a friendship to class or class templates, function or function templates, or types.<\/p>\n<h2>General Friendship<\/h2>\n<p>A class or a class template can grant friendship to each instance of a class template or a function template.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ generalFriendship.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>                            <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> myFriendFunction(T);\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> U<span style=\"color: #555555;\">&gt;<\/span>                           <em><span style=\"color: #0099ff;\"> \/\/ (2)<\/span><\/em>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyFriend<\/span>;\n\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">GrantingFriendshipAsClass<\/span> {\n\n  <span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> U<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> myFriendFunction(U);\n  <span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> U<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyFriend<\/span>;\n\n  std<span style=\"color: #555555;\">::<\/span>string secret{<span style=\"color: #cc3300;\">\"Secret from GrantingFriendshipAsClass.\"<\/span>};\n\n};\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">GrantingFriendshipAsClassTemplate<\/span>{\n\n  <span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> U<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> myFriendFunction(U);\n  <span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> U<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyFriend<\/span>;\n\n  std<span style=\"color: #555555;\">::<\/span>string secret{<span style=\"color: #cc3300;\">\"Secret from GrantingFriendshipAsClassTemplate.\"<\/span>};\n\n};\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;                                <em><span style=\"color: #0099ff;\">\/\/ (3)<\/span><\/em><\/span>\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> myFriendFunction(T){\n  GrantingFriendshipAsClass myFriend;\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myFriend.secret <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n  GrantingFriendshipAsClassTemplate<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> myFriend1;\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myFriend1.secret <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n}\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;                              <em><span style=\"color: #0099ff;\">\/\/ (4)<\/span><\/em><\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyFriend<\/span>{\n<span style=\"color: #9999ff;\">public:<\/span>\n  MyFriend(){\n    GrantingFriendshipAsClass myFriend;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myFriend.secret <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    GrantingFriendshipAsClassTemplate<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> myFriend1;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myFriend1.secret <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  }\n};\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\n\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> a{<span style=\"color: #ff6600;\">2011<\/span>};\n  myFriendFunction(a);\n\n  MyFriend<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> myFriend;\n\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n}\n<\/pre>\n<\/div>\n<p>Line (1) and line (2) forward declare the function template <code>myFriendFunction<\/code> and the class template<code> MyFriend.<\/code> The function template <code>myFriendFunction<\/code> is defined in line (3), and the class template <code>MyFriend<\/code> in line (4). The classes <code>GrantingFriendshipAsClass<\/code> and <code>GrantingFriendshipAsClassTemplate<\/code> grant the function template <code>myFriendFunction <\/code>and the class template<code> MyFriend <\/code>friendship<code>. <\/code>Due to the friendship, both templates can directly invoke the private member <code>secrete <\/code>of the class and the class template.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6227\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/generalFriendship.png\" alt=\"generalFriendship\" width=\"500\" height=\"257\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/generalFriendship.png 547w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/generalFriendship-300x154.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><code><br \/>\n<\/code><\/p>\n<p>There is a pitfall involved in the class template <code>GrantingFriendShipAsClassTemplate<\/code>. Usually, you call the first type parameter of a template<code> T<\/code>. An error occurs when you use &#8211; such as in the following code snippet &#8211; the same type parameter name for the class template and the function template<br \/>\nor the class template. The name <code>T<\/code> of <code>myFriendFunction<\/code> or <code>MyFriend<\/code> shadows the name<code> T<\/code> of the class template <code>GrantingFriendshipAsClassTemplate<\/code>.<\/p>\n<p>The following code snippet displays the pitfall.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">GrantingFriendshipAsClassTemplate<\/span>{\n\n  <span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> myFriendFunction(T);\n  <span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyFriend<\/span>;\n\n  std<span style=\"color: #555555;\">::<\/span>string secret{<span style=\"color: #cc3300;\">\"Secret from GrantingFriendshipAsClassTemplate.\"<\/span>};\n\n};\n<\/pre>\n<\/div>\n<h2>Special Friendship<\/h2>\n<p>A special friendship is a friendship that depends on the type of template parameter.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ specialFriendship.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> myFriendFunction(T);\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> U<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyFriend<\/span>;\n\n\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">GrantingFriendshipAsClass<\/span> {\n\n  <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> myFriendFunction<span style=\"color: #555555;\">&lt;&gt;<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>);             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n  <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyFriend<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>;                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n\n<span style=\"color: #9999ff;\">private:<\/span>\n  std<span style=\"color: #555555;\">::<\/span>string secret{<span style=\"color: #cc3300;\">\"Secret from GrantingFriendshipAsClass.\"<\/span>};\n\n};\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">GrantingFriendshipAsClassTemplate<\/span> {\n\n  <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> myFriendFunction<span style=\"color: #555555;\">&lt;&gt;<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>);\n  <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyFriend<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>;\n  <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyFriend<\/span><span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>;                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n\n<span style=\"color: #9999ff;\">private:<\/span>\n  std<span style=\"color: #555555;\">::<\/span>string secret{<span style=\"color: #cc3300;\">\"Secret from GrantingFriendshipAsClassTemplate.\"<\/span>};\n\n};\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> myFriendFunction(T) {\n  GrantingFriendshipAsClass myFriend;\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myFriend.secret <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n\n  GrantingFriendshipAsClassTemplate<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> myFriend1;\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myFriend1.secret <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\n}\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;                               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span><\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyFriend<\/span> {                      \n<span style=\"color: #9999ff;\">public:<\/span>\n  MyFriend() {\n    GrantingFriendshipAsClass myFriend;                 \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myFriend.secret <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    GrantingFriendshipAsClassTemplate<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myFriendInt;  \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myFriendInt.secret <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    GrantingFriendshipAsClassTemplate<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> myFriendT;      \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myFriendT.secret <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n  }\n};\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> a{<span style=\"color: #ff6600;\">2011<\/span>};\n  myFriendFunction(a);                                    \n\n  MyFriend<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myFriend;                                 \n\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n}\n<\/pre>\n<\/div>\n<p>The class<code> GrantingFriendshipAsClass<\/code> grants friendship to the full specialization of the function template <code>myFriendFunction<\/code> for <code>int<\/code> (line 1) and the class template <code>MyFriend<\/code> for <code>int<\/code> (line 2). The same holds for the class template <code>GrantingFrandshipAsClassTemplate<\/code>. Lines (3) is unique because it grants friendship to the full specialization for <code>MyFriend<\/code> having the same type parameter as the class template <code>GrantingFrandshipAsClassTemplate. <\/code>Consequently, the function template <code>myFriendFunction<\/code> can invoke the secret of the class <code>GrantingFriendshipAsClass <\/code>when <code>myFriendFunctions<\/code> is a full specialization for <code>int<\/code> (line 4) or <code>GrantingFriendshipAsClassTemplate<\/code> has the same type, such as <code>myFriendFunction<\/code> (line 5). The corresponding argumentation holds for the class template <code>MyFriend<\/code> (line 6).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6228\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/specialFriendship.png\" alt=\"specialFriendship\" width=\"450\" height=\"245\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/specialFriendship.png 538w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/specialFriendship-300x163.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<h2>Friend to Types<\/h2>\n<p>A class template can also grant its friendship to a type parameter.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ typeFriendship.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Bank<\/span> {\n    std<span style=\"color: #555555;\">::<\/span>string secret{<span style=\"color: #cc3300;\">\"Secret from the bank.\"<\/span>};\n    <span style=\"color: #006699; font-weight: bold;\">friend<\/span> T;\n};\n\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Account<\/span>{\n <span style=\"color: #9999ff;\">public:<\/span>\n    Account() {\n        Bank<span style=\"color: #555555;\">&lt;<\/span>Account<span style=\"color: #555555;\">&gt;<\/span> bank;\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> bank.secret <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;  <span style=\"color: #0099ff;\"> \/\/ (1)<\/span>\n    }\n};\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    Account acc;\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n}\n<\/pre>\n<\/div>\n<p>The class <code>Bank<\/code> grants friendship to its type parameter T. Consequently, an <code>Account<\/code> can access the secret of the <code>bank<\/code> instantiation for <code>Account<\/code>:<code> Bank&lt;Account<\/code>&gt; (line 1).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6229\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/typeFriendship.png\" alt=\"typeFriendship\" width=\"400\" height=\"206\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/typeFriendship.png 423w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/typeFriendship-300x155.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>In my next post, I will write about dependent names, one of the more complicated corners of templates.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A friend has unrestricted access to the members of a class. Consequently, friendship should be given wisely. Regarding templates, friendship is special.<\/p>\n","protected":false},"author":21,"featured_media":6208,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[439],"class_list":["post-6230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates","tag-friend"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6230"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6230\/revisions"}],"predecessor-version":[{"id":9761,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6230\/revisions\/9761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6208"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}