There is a CMake script:

unset(FOO) list(APPEND FOO "") list(APPEND FOO "") list(APPEND FOO "") list(APPEND FOO "") list(LENGTH FOO SIZE) message(FATAL_ERROR ${SIZE}) 

Waiting - 4. Reality - 0.

I found a workaround, but it looks like a crutch:

 set(FOO "dummy") set(FOO "${FOO};") set(FOO "${FOO};") set(FOO "${FOO};") set(FOO "${FOO};") list(REMOVE_AT FOO 0) message(FATAL_ERROR ${SIZE}) 

The example is somewhat simpler than what I actually do, so don’t suggest writing set(FOO ";;;") :) Elements come from outside in an unknown quantity, and only some of them are empty. Like this:

 function(do_smth LIST_WITH_EMPTY_ITEMS) unset(RESULT) foreach(X IN LISTS LIST_WITH_EMPTY_ITEMS) if(${CONDITION}) list(APPEND RESULT "${X}") endif() endforeach() endfunction() 
  • And why not agree on some element that it will be empty? Why do you need emptiness? - ixSci
  • @ixSci, I want to create a function that returns every nth item. nth_items("1;2;3;4;5;6;7" 1 3 RESULT) #RESULT = 1;4;7 . And I do not want to "swallow" empty elements - yrHeTaTeJlb
  • one
    It is just possible that adding an empty item in CMake will fail, because the list in CMake is essentially a string. Therefore, the concept of an empty element does not quite apply to it. There are only crutches, like what you have in question or what I wrote. - ixSci

0