diff --git a/README.adoc b/README.adoc index d89df7f..7f36b1c 100644 --- a/README.adoc +++ b/README.adoc @@ -12983,7 +12983,10 @@ Programs under link:userland/cpp/[] are examples of https://en.wikipedia.org/wik * link:userland/cpp/empty.cpp[] * link:userland/cpp/hello.cpp[] * templates -** link:userland/cpp/template.cpp[] +** link:userland/cpp/template.cpp[]: basic example +** link:userland/cpp/if_constexpr.cpp[]: C++17 `if constexpr` +*** https://stackoverflow.com/questions/12160765/if-else-at-compile-time-in-c/54647315#54647315 +*** https://stackoverflow.com/questions/37617677/implementing-a-compile-time-static-if-logic-for-different-string-types-in-a-co [[cpp-multithreading]] ==== C++ multithreading diff --git a/userland/cpp/if_constexpr.cpp b/userland/cpp/if_constexpr.cpp new file mode 100644 index 0000000..3e26120 --- /dev/null +++ b/userland/cpp/if_constexpr.cpp @@ -0,0 +1,23 @@ +// https://cirosantilli.com/linux-kernel-module-cheat#cpp + +#if __cplusplus >= 201703L +#include +#include + +template +struct MyClass { + int myFunc() { + if constexpr(std::is_integral()) + return 1; + else + return 2; + } +}; +#endif + +int main() { +#if __cplusplus >= 201703L + assert(MyClass().myFunc() == 1); + assert(MyClass().myFunc() == 2); +#endif +}