c++: template class with static member

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-09-07 00:00:00 +00:00
parent cb3f2f6183
commit e1ceb85934
2 changed files with 21 additions and 0 deletions

View File

@@ -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

View File

@@ -0,0 +1,20 @@
// https://cirosantilli.com/linux-kernel-module-cheat#cpp
#include <cassert>
template <class T>
struct MyClass {
static int i;
MyClass() {
i++;
}
};
template <class T>
int MyClass<T>::i = 0;
int main() {
MyClass<int>();
MyClass<int>();
assert(MyClass<int>::i == 2);
}