Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes using lambda functions to simulate lexical clousures have unforseen performace / implementation issues?
    primarykey
    data
    text
    <p><strong>Program 1:</strong></p> <pre><code>#include &lt;iostream&gt; std::string Hello(void){return "Hello";} std::string World(void){return "world!";} int main(){ std::cout &lt;&lt; Hello() &lt;&lt; " " &lt;&lt; World() &lt;&lt; std::endl; } </code></pre> <p>The functions Hello() and World() exist in the global namespace.<br> This means they can be called by any other function that comes after their declaration (or in this case, I provided the definition and did not need the declaration first). </p> <p>I have a problem with this, because as my project gets bigger, I include more header files that fill the global namespace with lots of functions, and I risk having function signature collisions, or worse, accidentally calling the wrong function that should only be called as a sub-task of another function. </p> <p>I am trying to follow the paradaim of functionally decomposing a task into sub-tasks, and thus it would not make sense for particular functions to ever be called outside the scope of another particular function. </p> <p>Here is a work-around, and apart from the code becoming unreadable due to the indentation depth, I want to know if there are any performance or implementation gotchas. lambda functions are a bit of magic for me at the moment, so I'm curious about the unforeseen dangers.</p> <p><strong>Program 2:</strong></p> <pre><code>#include &lt;iostream&gt; int main(){ auto Hello = [](void) -&gt; std::string{return "Hello";}; auto World = [](void) -&gt; std::string{return "world!";}; std::cout &lt;&lt; Hello() &lt;&lt; " " &lt;&lt; World() &lt;&lt; std::endl; } </code></pre> <p>Hello() and World() are encapsulated inside main() and <strong>can not</strong> be called from outside main()'s scope.<br> Is that all that's different?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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