# Is this really a better style to use generator expressions every time?

**URL:** https://discourse.cmake.org/t/is-this-really-a-better-style-to-use-generator-expressions-every-time/2753
**Category:** Usage
**Created:** [February 12, 2021, 12:47pm UTC](https://discourse.cmake.org/t/is-this-really-a-better-style-to-use-generator-expressions-every-time/2753 "2021-02-12T12:47:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [February 12, 2021, 12:47pm UTC](https://discourse.cmake.org/t/is-this-really-a-better-style-to-use-generator-expressions-every-time/2753/1 "2021-02-12T12:47:26Z")

</div>

```cmake
# either
target_compile_options(Greeter PUBLIC "$<$<BOOL:${MSVC}>:/permissive->")
# or
if(MSVC)
   target_compile_options(Greeter PUBLIC /permissive)
endif()

```

the first line generates on unix the `GreeterTarget.cmake` contents:

```cmake
# Create imported target Greeter::Greeter
add_library(Greeter::Greeter STATIC IMPORTED)

set_target_properties(Greeter::Greeter PROPERTIES
  INTERFACE_COMPILE_OPTIONS "\$<\$<BOOL:>:/permissive->"
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include/Greeter-1.0"
)

```

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [February 12, 2021, 12:55pm UTC](https://discourse.cmake.org/t/is-this-really-a-better-style-to-use-generator-expressions-every-time/2753/2 "2021-02-12T12:55:37Z")

</div>

These two options are 100% equivalent, because `${MSVC}` in the genex will be evaluated immediately (i.e. at configure time). So it comes down fully to code style preference, which is subjective (but I find the `if`-based form more readable).

For a case where configure-time vs. generate-time can be different, see a [recent question of mine](https://discourse.cmake.org/t/use-variables-or-genexes-with-usage-requirements/2679).

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [February 12, 2021, 1:18pm UTC](https://discourse.cmake.org/t/is-this-really-a-better-style-to-use-generator-expressions-every-time/2753/3 "2021-02-12T13:18:27Z")

</div>

But the generator expression pollute on `unix` hosts the installed `cmake config package`!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 12, 2021, 3:12pm UTC](https://discourse.cmake.org/t/is-this-really-a-better-style-to-use-generator-expressions-every-time/2753/4 "2021-02-12T15:12:07Z")

</div>

Eh, “no one” looks at those. In any case, the right thing, especially for compiler and linker flags, is to use a genex. The consumer of your package might not be using MSVC. MinGW isn’t going to understand `/permissive`. You should do:

```cmake
target_compile_options(Greeter
  PUBLIC
    "$<$<COMPILE_LANG_AND_ID:C,MSVC>:/permissive>") # or CXX

```
