This commit is contained in:
parent
f26a23b409
commit
8644965433
@ -22,6 +22,7 @@ steps:
|
||||
- cd misc || exit
|
||||
- g++ sizeof.cpp -o sizeof && ./sizeof
|
||||
- g++ caching.cpp -o caching && ./caching
|
||||
- g++ template.cpp -o template && ./template
|
||||
- echo "following program fails to show the effect of 'likely' due to CPU branch predictor."
|
||||
- g++ likely.cpp -o likely && ./likely
|
||||
- cd -
|
49
misc/template.cpp
Normal file
49
misc/template.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
int32_t getInt() {
|
||||
return INT32_MAX;
|
||||
}
|
||||
|
||||
int64_t getInt64() {
|
||||
return INT64_MAX;
|
||||
}
|
||||
|
||||
void getStr(string& str) {
|
||||
str = "abc";
|
||||
}
|
||||
void getVInt(vector<int>& vint) {
|
||||
vint = {1, 2, 3};
|
||||
}
|
||||
void getVStr(vector<string>& vstr) {
|
||||
vstr = {"abc", "def"};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T getVal() {
|
||||
T val;
|
||||
T& rVal = val;
|
||||
if (typeid(T) == typeid(int)) {
|
||||
(int&)rVal = getInt();
|
||||
} else if (typeid(T) == typeid(int64_t)) {
|
||||
(int64_t&)rVal = getInt64();
|
||||
} else if (typeid(T) == typeid(string)) {
|
||||
getStr((string&)rVal);
|
||||
} else if (typeid(T) == typeid(vector<int>)) {
|
||||
getVInt((vector<int>&)rVal);
|
||||
} else if (typeid(T) == typeid(vector<string>)) {
|
||||
getVStr((vector<string>&)rVal);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
int main() {
|
||||
assert(getVal<int32_t>() == INT32_MAX);
|
||||
assert(getVal<int64_t>() == INT64_MAX);
|
||||
assert(getVal<string>() == "abc");
|
||||
assert(getVal<vector<int>>() == vector<int>({1, 2, 3}));
|
||||
assert(getVal<vector<string>>() == vector<string>({"abc", "def"}));
|
||||
}
|
Loading…
Reference in New Issue
Block a user