Generic swap: Difference between revisions

Content deleted Content added
→‎{{header|CMake}}: Add 16th CMake example.
→‎{{header|CMake}}: Show 2 limitations of swap().
Line 209:
 
<lang cmake>function(swap var1 var2)
set(t_SWAP_TEMPORARY "${${var1}}")
set(${var1} "${${var2}}" PARENT_SCOPE)
set(${var2} "${t_SWAP_TEMPORARY}" PARENT_SCOPE)
endfunction(swap)</lang>
 
Line 219:
message(STATUS ${x}) # -- string
message(STATUS ${y}) # -- 42</lang>
 
Because of limitations in CMake, there are a few specific situations where swap() will fail to swap the variables.
 
# When ''_SWAP_TEMPORARY'' is the name of the second variable: <lang cmake>set(x 42)
set(_SWAP_TEMPORARY "string")
swap(x _SWAP_TEMPORARY)
message(STATUS ${x}) # -- 42
message(STATUS ${_SWAP_TEMPORARY}) # -- 42</lang> Inside swap(), its local variable ''_SWAP_TEMPORARY'' shadows the original ''_SWAP_TEMPORARY'' from the parent scope, preventing access to the original value.
# When value of either variable is "CACHE" or "PARENT_SCOPE": <lang cmake>string(TOUPPER CACHE x)
set(y "string")
swap(x y) # CMake Error... set given invalid arguments for CACHE mode.</lang> swap() can never set a variable to "CACHE" or "PARENT_SCOPE", because these are keywords of set() command.
 
=={{header|Common Lisp}}==