diff --git a/README.adoc b/README.adoc index 7f36b1c..58dd47e 100644 --- a/README.adoc +++ b/README.adoc @@ -12984,6 +12984,7 @@ Programs under link:userland/cpp/[] are examples of https://en.wikipedia.org/wik * link:userland/cpp/hello.cpp[] * templates ** link:userland/cpp/template.cpp[]: basic example +** link:userland/cpp/template_class_with_static_member.cpp[]: https://stackoverflow.com/questions/3229883/static-member-initialization-in-a-class-template ** 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 diff --git a/userland/cpp/template_class_with_static_member.cpp b/userland/cpp/template_class_with_static_member.cpp new file mode 100644 index 0000000..33f3a8d --- /dev/null +++ b/userland/cpp/template_class_with_static_member.cpp @@ -0,0 +1,20 @@ +// https://cirosantilli.com/linux-kernel-module-cheat#cpp + +#include + +template +struct MyClass { + static int i; + MyClass() { + i++; + } +}; + +template +int MyClass::i = 0; + +int main() { + MyClass(); + MyClass(); + assert(MyClass::i == 2); +}