Well you&#39;re really comparing apples to oranges. C++ nested scoping rules really have nothing to do with two separate functions sharing scoped variables. It doesn&#39;t even really serve as a good analogy, so I can&#39;t be 100% certain what you were trying to tell me ;-)<div>
<br></div><div>However I appreciate your response. I really just wanted to make sure this isn&#39;t a bug, because the way the called function inherits the calling function&#39;s local variables is an unusual behavior for a language, at least in my experience. So I had to think twice about it ;)</div>
<div><div><br></div><div>---------</div>Robert Dailey<br>
<br><br><div class="gmail_quote">On Fri, Mar 2, 2012 at 6:53 PM, Michael Hertling <span dir="ltr">&lt;<a href="mailto:mhertling@online.de">mhertling@online.de</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 03/01/2012 06:01 PM, Robert Dailey wrote:<br>
&gt; No, the print statement is not missing. In fact it prints just fine<br>
&gt; (function test() is able to obtain the value for variable SOME_TEST).<br>
<br>
</div>I meant the output &quot;SOME_TEST: HELLO WORLD&quot; was missing in your report.<br>
<div class="im"><br>
&gt; This isn&#39;t exactly the same as C++. In C++, a function does not have access<br>
&gt; to the calling function&#39;s local declarations. In order for the function to<br>
&gt; get access to these, they must be passed in as parameters.<br>
<br>
</div>There wasn&#39;t talk of functions but of the &quot;{&quot; and &quot;}&quot; tokens:<br>
<br>
#include &lt;stdio.h&gt;<br>
<br>
int main(void)<br>
{<br>
    int outer = 1;<br>
    {<br>
        int inner = 2;<br>
        printf(&quot;outer=%d, inner=%d\n&quot;,outer,inner);<br>
    }<br>
    return 0;<br>
}<br>
<br>
As you will see, there&#39;s access to the outer scope from the inner one.<br>
<br>
However, to be more precise, C/C++&#39;s scoping is static, i.e. it&#39;s in<br>
effect at compilation time only, and - in contrast to Pascal, e.g. -<br>
the ISO dialects don&#39;t allow a nested definition of functions, so a<br>
function&#39;s scope isn&#39;t part of another function&#39;s one. Therefore, a<br>
called function can&#39;t see the variables of the calling function as<br>
their scopes aren&#39;t descendants but siblings.<br>
<br>
Interpreted languages like CMake&#39;s one often have dynamic scoping,<br>
i.e. an invocation of a function creates a new scope - capable to<br>
hold variables - and pushes it on a stack from where it is popped<br>
and destroyed when the function terminates. When dereferencing a<br>
variable, it is searched by traversing the stacked scopes from<br>
inner to outer until it is found. Therefore, a called function<br>
can see the calling function&#39;s variables since the latter&#39;s<br>
scope is accessible from the former&#39;s one.<br>
<br>
Maybe, it was a rather bad idea to compare C/C++&#39;s static<br>
block scoping with CMake&#39;s dynamic function scoping.<br>
Sorry about that.<br>
<br>
Regards,<br>
<br>
Michael<br>
<div class="HOEnZb"><div class="h5"><br>
&gt; On Wed, Feb 29, 2012 at 9:54 PM, Michael Hertling &lt;<a href="mailto:mhertling@online.de">mhertling@online.de</a>&gt;wrote:<br>
&gt;<br>
&gt;&gt; On 03/01/2012 01:38 AM, Robert Dailey wrote:<br>
&gt;&gt;&gt; I ran a quick test:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; function( test )<br>
&gt;&gt;&gt; message( &quot;SOME_TEST: ${SOME_TEST}&quot; )<br>
&gt;&gt;&gt; endfunction()<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; function( start )<br>
&gt;&gt;&gt; set( SOME_TEST &quot;HELLO WORLD&quot; )<br>
&gt;&gt;&gt; test()<br>
&gt;&gt;&gt; endfunction()<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; start()<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Seems like a function has access to the calling scope&#39;s defined<br>
&gt;&gt; variables.<br>
&gt;&gt;&gt; I thought because functions created a new scope, that excluded access to<br>
&gt;&gt;&gt; variables defined in the outer scope (i.e. calling scope)<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Can someone explain?<br>
&gt;&gt;<br>
&gt;&gt; The line &quot;SOME_TEST: HELLO WORLD&quot; is missing, I guess?<br>
&gt;&gt;<br>
&gt;&gt; As usual with scoping mechanisms, there is access to the outer scope<br>
&gt;&gt; from within the inner scope: Read access via ordinary dereferencing<br>
&gt;&gt; and write access via PARENT_SCOPE. It&#39;s quite the same as in C/C++<br>
&gt;&gt; with the { and } tokens; see also the C++ &quot;::&quot; operator.<br>
&gt;&gt;<br>
&gt;&gt; Regards,<br>
&gt;&gt;<br>
&gt;&gt; Michael<br>
--<br>
<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.cmake.org/mailman/listinfo/cmake" target="_blank">http://www.cmake.org/mailman/listinfo/cmake</a><br>
</div></div></blockquote></div><br></div>