奇怪的函数参数名称行为 [英] Strange function argument name behaviour
问题描述
我问了一个关于cmake和传递变量的问题此处。我可以使其正常工作,但前提是我在函数中使用的变量名与为该函数调用的父作用域中的变量不同。因此,从本质上讲:
I asked a questions about cmake and passing variables here. I can make it work, but only if I name my variable in the function differently than the variable in parent scope I call the function for. So in essence:
function(strange name)
message(STATUS ${name})
message(STATUS ${${name}})
endfunction()
set(name foo)
set(anothername foo)
strange(name)
strange(anothername)
哪个会导致:
-- name (message(STATUS ${name}) for var "name")
-- name (message(STATUS ${${name}}) for var "name")
-- anothername message(STATUS ${name}) for var "anothername")
-- foo (message(STATUS ${${name}}) for var "anothername")
那不是很奇怪吗?发生了什么?
我认为函数的行为不应依赖于父作用域中变量的命名-应该吗?!
Isn't that a little weird? What's happening? I think the behaviour of a function should not depend on the naming of variable in the parent scope - should it?!
任何澄清都值得赞赏! / p>
Any clarification is much appreciated!
推荐答案
不幸的是,CMake的语言非常原始。在这种情况下,局部变量的命名可能会与外部作用域中具有相同名称的变量进行交互。
Unfortunately, the language of CMake is extremely primitive. In this case, the naming of local variables may interact with variables with the same name in outer scopes.
具体地,在两个调用中, $ { $ {name}}
扩展为 $ {name}
和 $ {anothername}
,分别。在前一个 name
中,是使用其值的局部变量的名称。在后者中,使用外部作用域中的别名
。
Concretely, in the two calls ${${name}}
expands to ${name}
and ${anothername}
, respectively. In the former name
is the name of a local variable whose value is used. In the latter, the anothername
from the outer scope is used.
我不知道
也许我们应该申请 $ {name:PARENT_SCOPE} ?
这篇关于奇怪的函数参数名称行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!