Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Type erasure
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
//Title of this code //g++ 4.8.2 #include <iostream> #include <vector> #include <memory> class Thing { private: struct Object { virtual void print(std::ostream&) const = 0; }; template<class T> struct ObjectModel : Object { ObjectModel(const T& obj) : obj_(obj) { }; void print(std::ostream& out) const { out << obj_; } T obj_; }; std::shared_ptr<Object> objPtr; public: explicit Thing(const char* str) : objPtr(new ObjectModel<std::string>(str)) { } template<class T> explicit Thing(const T& obj) : objPtr(new ObjectModel<T>(obj)) { } void print(std::ostream& out) const { objPtr->print(out); } }; std::ostream& operator << (std::ostream& out, const Thing& thing) { thing.print(out); return out; } class Person { private: std::string firstName_; std::string familyName_; public: Person(const std::string& firstName, const std::string& familyName) : firstName_(firstName), familyName_(familyName) { } std::string getFirstName() const { return firstName_; } std::string getFamilyName() const { return familyName_; } void print(std::ostream& out) const { out << "[First Name: " << getFirstName() << ", Family name: " << getFamilyName() << "]"; } }; std::ostream& operator << (std::ostream& out, const Person& person) { person.print(out); return out; } int main() { std::vector<Thing> things; things.push_back(Thing(1)); things.push_back(Thing(std::string("hello"))); things.push_back(Thing(5.5)); things.push_back(Thing(Person("Ovidiu", "Moscaliuc"))); things.push_back(Thing("nice")); for(unsigned int i = 0; i < things.size(); ++ i) { std::cout << things[i] << " "; } }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.82 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.97 sec
edit mode
|
history
|
discussion
1 hello 5.5 [First Name: Ovidiu, Family name: Moscaliuc] nice