The other difference (besides scoping) between a macro and a function explains the behavior you are seeing:<div><br></div><div>The parameters to a function are automatically local variables inside the function scope. The parameters to a macro are not actually variables. They are merely expanded *as if* they were variables anywhere you see the ${} de-referencing syntax. Think of it like #define in C++ : the arguments are substituted where they are referenced, but they are not actually local variables.<br>
<br></div><div>It's equivalent to the difference between these in C++:</div><div><br></div><div>// there are not actually variables named x and y here:</div><div>#define min(x, y) ......</div><div><br></div><div>// but there are variables here:</div>
<div>int min(int x, int y)</div><div>{</div><div> .......</div><div>}</div><div><br></div><div><br></div><div>HTH,</div><div>David</div><div><br></div><div><br></div><div><br><div class="gmail_quote">2009/5/15 Nicolas Desprès <span dir="ltr"><<a href="mailto:nicolas.despres@gmail.com">nicolas.despres@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi there,<br>
<br>
I'm experiencing a weird behavior with cmake 2.6.4 (Win32) playing<br>
with macro, function and ARGN.<br>
<br>
=== script<br>
function(print_var)<br>
foreach(i ${ARGV})<br>
message("${i}='${${i}}'")<br>
endforeach(i)<br>
endfunction(print_var)<br>
<br>
macro(print_var_macro)<br>
foreach(i ${ARGV})<br>
message("macro ${i}='${${i}}'")<br>
endforeach(i)<br>
endmacro(print_var_macro)<br>
<br>
function(foo a1 a2)<br>
message("begin foo")<br>
message("a1=${a1}")<br>
message("a2=${a2}")<br>
message("ARGN=${ARGN}")<br>
print_var(a1 a2 ARGN) # 1<br>
print_var_macro(a1 a2 ARGN) # 2<br>
message("end foo")<br>
endfunction(foo)<br>
<br>
macro(bar a1 a2)<br>
message("begin bar")<br>
message("a1=${a1}")<br>
message("a2=${a2}")<br>
message("ARGN=${ARGN}")<br>
print_var(a1 a2 ARGN) # 3<br>
print_var_macro(a1 a2 ARGN) # 4<br>
message("end bar")<br>
endmacro(bar)<br>
<br>
foo("a" "b" "c" "d" "e")<br>
bar("a" "b" "c" "d" "e")<br>
=== end script<br>
=== output<br>
begin foo<br>
a1=a<br>
a2=b<br>
ARGN=c;d;e<br>
a1='a'<br>
a2='b'<br>
ARGN='a1;a2;ARGN'<br>
macro a1='a'<br>
macro a2='b'<br>
macro ARGN='c;d;e'<br>
end foo<br>
begin bar<br>
a1=a<br>
a2=b<br>
ARGN=c;d;e<br>
a1=''<br>
a2=''<br>
ARGN='a1;a2;ARGN'<br>
macro a1=''<br>
macro a2=''<br>
macro ARGN=''<br>
end bar<br>
=== end output<br>
<br>
1) I undestand it perfectly. ARGN is interpreted in the context of the<br>
`print_var' function.<br>
2) I don't understand why the behavior is not similar to #1<br>
3) I don't understand why `a1' and `a2' are "eaten"<br>
4) This one is very weird.<br>
<br>
I though the only difference between macro and function was that<br>
variables are scoped in a function and not in a macro. In all cases, I<br>
find those behavior weird compared to what I'm used to in other<br>
languages.<br>
<br>
Any light?<br>
<br>
Cheers,<br>
<br>
--<br>
Nicolas Desprès<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>
</blockquote></div><br></div>