Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The preferred mechanic in C++ to achieve what you want is to make things (functions, types, objects) private (not in the sense of accessibility, unlike the keyword) to a module, such that they cannot be named in another. This prevents collisions altogether.</p> <p>For instance, consider a module consisting of a header file:</p> <pre><code>// List dependencies #include &lt;string&gt; // It's customary to put things in a namespace // Note that namespaces are commonly cross-module namespace ns { // Declarations visible to all std::string hello_world(); } // ns </code></pre> <p>and such an implementation:</p> <pre><code>// include header listed above #include &lt;header.hpp&gt; // This contains declarations visible to this module only namespace { std::string hello_part(); // This is both a declaration and a definition std::string world_part() { return "world!"; } } // namespace // Definition of function declared in header std::string ns::hello_world() { // Implemention defers to private parts that are visible here return hello_part() + " " + world_part(); } // Definition of private function // We need to reopen the unnamed namespace for that namespace { std::string hello_part() { return "Hello"; } } // namespace </code></pre> <p>Now another module can declare another <code>hello_part</code> on their own, which needn't be a function with the same declaration or even be a function at all, and there won't be a conflict. There will be two different and separate entities -- much like a <code>for</code> statement can have their own <code>int i;</code> variable with interfering without any other <code>i</code>!</p> <p>As for the other part of your question, it doesn't matter much if the <code>hello_part</code> and <code>world_part</code> above are implemented as plain old functions or as function objects (via lambda expressions or otherwise). But keep in mind that since they don't close over anything, they're not much of a closure. C++ lambda expressions do allow for closing over anything, including local variables (also known as local captures), but the type system is not quite expressive enough to allow for the <a href="https://en.wikipedia.org/wiki/Funarg_problem" rel="nofollow">upwards funarg problem</a> without paying a small penalty in certain situations. True to the C++ creed though, this can't happen behind your back, and you will need to spell out that penalty via e.g. <code>std::function</code>. For those cases I would argue against lambda expressions as I think there are better alternatives (on which I won't expand here).</p> <p>In general though, deciding whether you want to use a lambda expression is a consideration that will not (and should not) affect modularity of your program.</p> <p>A longer and more detailed answer would explain the different roles of scopes and visibility, declarations and implementations, namespaces, modules, and accessibility which can all be used to make a program modular. It is best not to conflate them. My example uses all of those things except for accessibility to achieve its needs, but it's far from the only way to do it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload