如何编写一个通过引用传递变量的漂亮函数? [英] How to write a nice function that passes variables by reference?

查看:87
本文介绍了如何编写一个通过引用传递变量的漂亮函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题中所述,我想在cmake中编写一个 nice函数,该函数能够修改作为参数传递给该函数的变量。

as in said in the title, I would like to write a "nice" function in cmake that is able to modify a variable which is passed as a parameter into that function.

我能想到的唯一方法是丑陋的:

The only way I can think of doing it is ugly:

函数定义

function(twice varValue varName)
set(${varName} ${varValue}${varValue} PARENT_SCOPE)
endfunction(twice)

用法

set(arg foo)
twice(${arg} arg)
message("arg = "${arg})

结果

arg = foofoo

在我看来,没有任何变量可以传递的真实概念?!
我觉得我尚未接受cmake的一些基本知识。

It seems to me, there is no real concept of variables that one can pass around at all?! I feel like there is something fundamental about cmake that I didn't take in yet.

那么,有没有更好的方法呢?

So, is there a nicer way to do this?

非常感谢!

推荐答案

您不需要传递值和变量名。名称就足够了,因为您可以通过名称访问值:

You don't need to pass the value and the name of the variable. The name is enough, because you can access the value by the name:

function(twice varName)
  SET(${varName} ${${varName}}${${varName}} PARENT_SCOPE)
endfunction()

SET(arg "foo")
twice(arg)
MESSAGE(STATUS ${arg})

输出 foofoo

这篇关于如何编写一个通过引用传递变量的漂亮函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆