{"id":20962,"date":"2018-11-20T14:41:27","date_gmt":"2018-11-20T14:41:27","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/vcblog\/?p=20962"},"modified":"2019-02-18T17:47:33","modified_gmt":"2019-02-18T17:47:33","slug":"qa-fine-grained-friendship","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/qa-fine-grained-friendship\/","title":{"rendered":"Q&amp;A: Fine-grained friendship"},"content":{"rendered":"<p><i>This <\/i><i>post <\/i><i>is part of a regular series of posts <\/i><i>where the C++ product team here at Microsoft answers questions we have received from customers. <\/i><i>The questions can be about anything C++ related: Visual C++, the standard language and library<\/i><i>, <\/i><i>the C++ standards committee, isocpp.org, <\/i><i>CppCon<\/i><i>, etc<\/i><i>.<\/i> <i>Today&#8217;s Q&amp;A is by Herb Sutter.<\/i><\/p>\n<h3>Question<\/h3>\n<p>Reader @thesamhughescom recently asked:<\/p>\n<blockquote>\n<p style=\"padding-left: 30px\">Has there ever been a consideration for allowing individual private functions to whitelist other classes or functions to call them? Similar to the per class friend method, I was thinking you could annotate a function with <code>[[friend void foo(int)]]<\/code> or <code>[[friend class baz]]<\/code> just an idea, I wouldn\u2019t know where to get started on writing my own proposal, thanks Sam<\/p>\n<\/blockquote>\n<h3>Answer<\/h3>\n<p>There are occasionally proposals, but <code>friend<\/code> itself should be used very rarely so there&#8217;s not a lot of motivation to encourage it.<\/p>\n<p>One technique you can use today is to provide a set of friend helper types, each of which provides specific access to a given subset of things and can name its own friends, be handed out to callers, etc. That&#8217;s a flexible form of access control that works for granting different degrees of friendship statically to a set of types, granting access to some parts of the class\u2019s interface dynamically to a set of callers, and so on.<\/p>\n<p>But the last part of your question is the easiest, and perhaps the most important:<\/p>\n<blockquote>\n<p style=\"padding-left: 30px\">I wouldn\u2019t know where to get started on writing my own proposal<\/p>\n<\/blockquote>\n<p>Always (always, always, \u2026) start with use cases. Write two or three concrete examples of the kind of code you want to write (initially pseudocode), explain clearly why you want to express it that way, and show the workarounds if any that are available for approximating it today. Compelling examples serve two major purposes: they motivate the feature (answer \u201cwhy\u201d questions like why have the features), and they also provide reference points to guide the feature&#8217;s design (answer \u201cwhat\u201d and \u201chow\u201d questions like what things the feature must be able to express and how it should be able to be used).<\/p>\n<h3>Your questions?<\/h3>\n<p><i>If you have any question about C++ in general, please comment about it below. Someone <\/i><i>in <\/i><i>the community may answer it, or someone on our team may consider it for a future blog post. If instead your question is about support for <\/i><i>a<\/i><i> Microsoft product<\/i><i>, <\/i><i>you can provide feedback via <\/i><a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio-2017\"><i>Help &gt; Report A Problem in the product<\/i><\/a><i>, or via <\/i><a href=\"https:\/\/developercommunity.visualstudio.com\/topics\/C%2B%2B.html\"><i>Developer Community<\/i><\/a><i>.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is part of a regular series of posts where the C++ product team here at Microsoft answers questions we have received from customers. The questions can be about anything C++ related: Visual C++, the standard language and library, the C++ standards committee, isocpp.org, CppCon, etc. Today&#8217;s Q&amp;A is by Herb Sutter. Question Reader [&hellip;]<\/p>\n","protected":false},"author":626,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[512],"tags":[],"class_list":["post-20962","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general-cpp-series"],"acf":[],"blog_post_summary":"<p>This post is part of a regular series of posts where the C++ product team here at Microsoft answers questions we have received from customers. The questions can be about anything C++ related: Visual C++, the standard language and library, the C++ standards committee, isocpp.org, CppCon, etc. Today&#8217;s Q&amp;A is by Herb Sutter. Question Reader [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/20962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/626"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=20962"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/20962\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=20962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=20962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=20962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}