mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
c++: template class with static member
This commit is contained in:
@@ -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
|
||||
|
||||
20
userland/cpp/template_class_with_static_member.cpp
Normal file
20
userland/cpp/template_class_with_static_member.cpp
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user