Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
a simple tuple implementation
#include <iostream> #include <memory> #include <cassert> #include<vector> #include<string> #include<typeinfo> using namespace std; // the Container class is storage for any type of variable class Container { public: template <typename T> Container (T& t) { up = std::make_unique<Wrapper<T>>(t); Ctype_ = const_cast<char*> ((Wrapper<T> (t)).Wtype_); } template <typename T> Container(T&& t) { up = std::make_unique<Wrapper<T>>(t); Ctype_ = const_cast<char*> ((Wrapper<T>(t)).Wtype_); } class Wrapper_base { public: virtual ~Wrapper_base() = default; }; template <typename T> class Wrapper : public Wrapper_base { public: ~Wrapper() override = default; Wrapper(const T& t) : t_(t) {} const char* Wtype_ = typeid(T).name(); private: T t_; }; std::unique_ptr<Wrapper_base> up; char* Ctype_; }; // a hetrogenous vector that can hold any type of variable regardless of its type class Tuple { private: vector<Container> vc; public: template <typename T> void push_back(T& t) { vc.push_back(Container(t)); } template <typename T> void push_back(T&& t) { vc.push_back(Container(t)); } Container& operator[](unsigned int index) { return vc[index]; } }; class f { public: int i = -100; }; int main() { Tuple t; int a = 1; t.push_back(a); t.push_back(1); string s = "Ayyyy!"; t.push_back(s); double d = 3.1415; t.push_back(d); t.push_back(3.1415); t.push_back(f()); cout << t[0].Ctype_ << endl; cout << t[1].Ctype_ << endl; cout << t[2].Ctype_ << endl; cout << t[3].Ctype_ << endl; cout << t[4].Ctype_ << endl; cout << t[5].Ctype_ << endl; }
run
|
edit
|
history
|
help
0
new
TIME
Test 2(2021)
obracanie tablicy
ThreadPool
Test 15(2020)
aqws
N Queens problem
articulation points and bridges
Wipro Problem 1