Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
a simple tuple implementation
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#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; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.62 sec, absolute running time: 0.07 sec, cpu time: 0.02 sec, memory peak: 3 Mb, absolute service time: 0,7 sec
edit mode
|
history
|
discussion
i i NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE d d 1f