diff --git a/.drone.yml b/.drone.yml index 938afd0..985d75e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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 - \ No newline at end of file + - cd - diff --git a/misc/template.cpp b/misc/template.cpp new file mode 100644 index 0000000..c6f8dc7 --- /dev/null +++ b/misc/template.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +using namespace std; + +int32_t getInt() { + return INT32_MAX; +} + +int64_t getInt64() { + return INT64_MAX; +} + +void getStr(string& str) { + str = "abc"; +} +void getVInt(vector& vint) { + vint = {1, 2, 3}; +} +void getVStr(vector& vstr) { + vstr = {"abc", "def"}; +} + +template +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)) { + getVInt((vector&)rVal); + } else if (typeid(T) == typeid(vector)) { + getVStr((vector&)rVal); + } + return val; +} + +int main() { + assert(getVal() == INT32_MAX); + assert(getVal() == INT64_MAX); + assert(getVal() == "abc"); + assert(getVal>() == vector({1, 2, 3})); + assert(getVal>() == vector({"abc", "def"})); +} \ No newline at end of file