mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
c++: if constexpr
This commit is contained in:
@@ -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/empty.cpp[]
|
||||||
* link:userland/cpp/hello.cpp[]
|
* link:userland/cpp/hello.cpp[]
|
||||||
* templates
|
* 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]]
|
[[cpp-multithreading]]
|
||||||
==== C++ multithreading
|
==== C++ multithreading
|
||||||
|
|||||||
23
userland/cpp/if_constexpr.cpp
Normal file
23
userland/cpp/if_constexpr.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// https://cirosantilli.com/linux-kernel-module-cheat#cpp
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
|
#include <cassert>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct MyClass {
|
||||||
|
int myFunc() {
|
||||||
|
if constexpr(std::is_integral<T>())
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
|
assert(MyClass<int>().myFunc() == 1);
|
||||||
|
assert(MyClass<float>().myFunc() == 2);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user