Well you're really comparing apples to oranges. C++ nested scoping rules really have nothing to do with two separate functions sharing scoped variables. It doesn't even really serve as a good analogy, so I can'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't a bug, because the way the called function inherits the calling function'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"><<a href="mailto:mhertling@online.de">mhertling@online.de</a>></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>
> No, the print statement is not missing. In fact it prints just fine<br>
> (function test() is able to obtain the value for variable SOME_TEST).<br>
<br>
</div>I meant the output "SOME_TEST: HELLO WORLD" was missing in your report.<br>
<div class="im"><br>
> This isn't exactly the same as C++. In C++, a function does not have access<br>
> to the calling function's local declarations. In order for the function to<br>
> get access to these, they must be passed in as parameters.<br>
<br>
</div>There wasn't talk of functions but of the "{" and "}" tokens:<br>
<br>
#include <stdio.h><br>
<br>
int main(void)<br>
{<br>
int outer = 1;<br>
{<br>
int inner = 2;<br>
printf("outer=%d, inner=%d\n",outer,inner);<br>
}<br>
return 0;<br>
}<br>
<br>
As you will see, there's access to the outer scope from the inner one.<br>
<br>
However, to be more precise, C/C++'s scoping is static, i.e. it's in<br>
effect at compilation time only, and - in contrast to Pascal, e.g. -<br>
the ISO dialects don't allow a nested definition of functions, so a<br>
function's scope isn't part of another function's one. Therefore, a<br>
called function can't see the variables of the calling function as<br>
their scopes aren't descendants but siblings.<br>
<br>
Interpreted languages like CMake'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's variables since the latter's<br>
scope is accessible from the former's one.<br>
<br>
Maybe, it was a rather bad idea to compare C/C++'s static<br>
block scoping with CMake's dynamic function scoping.<br>
Sorry about that.<br>
<br>
Regards,<br>
<br>
Michael<br>
<div class="HOEnZb"><div class="h5"><br>
> On Wed, Feb 29, 2012 at 9:54 PM, Michael Hertling <<a href="mailto:mhertling@online.de">mhertling@online.de</a>>wrote:<br>
><br>
>> On 03/01/2012 01:38 AM, Robert Dailey wrote:<br>
>>> I ran a quick test:<br>
>>><br>
>>><br>
>>> function( test )<br>
>>> message( "SOME_TEST: ${SOME_TEST}" )<br>
>>> endfunction()<br>
>>><br>
>>> function( start )<br>
>>> set( SOME_TEST "HELLO WORLD" )<br>
>>> test()<br>
>>> endfunction()<br>
>>><br>
>>> start()<br>
>>><br>
>>><br>
>>> Seems like a function has access to the calling scope's defined<br>
>> variables.<br>
>>> I thought because functions created a new scope, that excluded access to<br>
>>> variables defined in the outer scope (i.e. calling scope)<br>
>>><br>
>>> Can someone explain?<br>
>><br>
>> The line "SOME_TEST: HELLO WORLD" is missing, I guess?<br>
>><br>
>> As usual with scoping mechanisms, there is access to the outer scope<br>
>> from within the inner scope: Read access via ordinary dereferencing<br>
>> and write access via PARENT_SCOPE. It's quite the same as in C/C++<br>
>> with the { and } tokens; see also the C++ "::" operator.<br>
>><br>
>> Regards,<br>
>><br>
>> 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>