c++ template class example

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-09-07 00:00:00 +00:00
parent 906f7ac625
commit 4806f0ecfb
2 changed files with 19 additions and 0 deletions

View File

@@ -12982,6 +12982,8 @@ 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[]
[[cpp-multithreading]]
==== C++ multithreading

17
userland/cpp/template.cpp Normal file
View File

@@ -0,0 +1,17 @@
// https://cirosantilli.com/linux-kernel-module-cheat#cpp
#include <cassert>
template <class T>
struct MyClass {
T myVal;
MyClass(T myVal) : myVal(myVal) {}
T myFunc() {
return myVal + 1;
}
};
int main() {
assert(MyClass<int>(1).myFunc() == 2);
assert(MyClass<float>(1.5).myFunc() == 2.5);
}