# IN\_LIST generator-expression does not work as expected

**URL:** <https://discourse.cmake.org/t/in-list-generator-expression-does-not-work-as-expected/5853>\
**Category:** Code\
**Created:** [June 13, 2022, 3:50am UTC](https://discourse.cmake.org/t/in-list-generator-expression-does-not-work-as-expected/5853 "2022-06-13T03:50:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin-peng-hao](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kevin-peng-hao/32/2493_2.png) [@kevin-peng-hao](https://discourse.cmake.org/u/kevin-peng-hao)\
**Post date:** [June 13, 2022, 3:50am UTC](https://discourse.cmake.org/t/in-list-generator-expression-does-not-work-as-expected/5853/1 "2022-06-13T03:50:26Z")

</div>

Hi, I have a piece of code like this:

```auto
set(SOME_LIST member_a meber_b)
set(TEST_STRING member_a)
if (${TEST_STRING} IN_LIST SOME_LIST)
    target_sources(a_library
        PRIVATE
            some_code_a.c
    )
else()
    target_sources(a_library
        PRIVATE
            some_code_b.c
    )
endif()

```

It works fine to pick up some\_code\_a.c.  
But if I convert it to generator-expression:

```auto
target_sources(a_library
    PRIVATE
$<$<IN_LIST:${TEST_STRING},SOME_LIST>:${CMAKE_CURRENT_SOURCE_DIR}/some_code_a.c>
$<$<NOT:$<IN_LIST:${TEST_STRING},SOME_LIST>>:${CMAKE_CURRENT_SOURCE_DIR}/some_code_b.c>
)

```

It always picks up some\_code\_b.c.  
I just couldn’t understand.  
Is there anyone who can help me out with this?

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [June 13, 2022, 6:04am UTC](https://discourse.cmake.org/t/in-list-generator-expression-does-not-work-as-expected/5853/2 "2022-06-13T06:04:24Z")

</div>

You have to expand your `SOME_LIST`:

```auto
target_sources(a_library
    PRIVATE
"$<$<IN_LIST:${TEST_STRING},${SOME_LIST}>:${CMAKE_CURRENT_SOURCE_DIR}/some_code_a.c>"
"$<$<NOT:$<IN_LIST:${TEST_STRING},${SOME_LIST}>>:${CMAKE_CURRENT_SOURCE_DIR}/some_code_b.c>"
)

```

---

<div class="post-metadata">

**Author:** ![kevin-peng-hao](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kevin-peng-hao/32/2493_2.png) [@kevin-peng-hao](https://discourse.cmake.org/u/kevin-peng-hao)\
**Post date:** [June 13, 2022, 6:17am UTC](https://discourse.cmake.org/t/in-list-generator-expression-does-not-work-as-expected/5853/3 "2022-06-13T06:17:41Z")

</div>

Thanks, Jakub.  
It worked！！!  
But… why?  
I mean, other generator expressions such as `NOT` and `STREQUAL` do not need to be expanded.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [June 13, 2022, 7:05am UTC](https://discourse.cmake.org/t/in-list-generator-expression-does-not-work-as-expected/5853/4 "2022-06-13T07:05:04Z")

</div>

AFAIK they do.  
That’s because the CMake variables do not exist anymore when the genex gets evaluated.

---

<div class="post-metadata">

**Author:** ![kevin-peng-hao](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kevin-peng-hao/32/2493_2.png) [@kevin-peng-hao](https://discourse.cmake.org/u/kevin-peng-hao)\
**Post date:** [June 13, 2022, 7:14am UTC](https://discourse.cmake.org/t/in-list-generator-expression-does-not-work-as-expected/5853/5 "2022-06-13T07:14:26Z")

</div>

Hmmm…  
There might be some miss understanding.  
By “expand", do you mean `${CMAKE_VAR}`?  
If it is, then expanding CMake variables for `IN_LIST` only does not work either.  
It’s the double quotation marks that fix my example.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [June 13, 2022, 7:37am UTC](https://discourse.cmake.org/t/in-list-generator-expression-does-not-work-as-expected/5853/6 "2022-06-13T07:37:50Z")

</div>

> [@kevin-peng-hao](#):
>
> By “expand", do you mean `${CMAKE_VAR}`?

Yes, that’s what I meant.  
The double Quotation is just another piece of the puzzle, which prevents premature splitting of the genex by the argument parser. Without it, after expansion of the list, the semicolons will mess things up.  
Typically you want your genex quoted like that.

---

<div class="post-metadata">

**Author:** ![kevin-peng-hao](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kevin-peng-hao/32/2493_2.png) [@kevin-peng-hao](https://discourse.cmake.org/u/kevin-peng-hao)\
**Post date:** [June 13, 2022, 10:02am UTC](https://discourse.cmake.org/t/in-list-generator-expression-does-not-work-as-expected/5853/7 "2022-06-13T10:02:45Z")

</div>

Thank again!

---

<div class="post-metadata">

**Author:** ![sjl3110](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sjl3110/32/2240_2.png) [@sjl3110](https://discourse.cmake.org/u/sjl3110)\
**Post date:** [June 14, 2022, 2:34am UTC](https://discourse.cmake.org/t/in-list-generator-expression-does-not-work-as-expected/5853/8 "2022-06-14T02:34:34Z")

</div>

Hi

Why BOOL/NOT/AND/OR doesn’t need **double Quotation**? If the **double Quotation** is necessary for **IN\_LIST** to avoid issue caused by semicolon in list, why not mention this in document or refine this defect in CMAKE source code?

Best Regards  
Jianliang Shen
