Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
Handling new types without using RTTI
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 <algorithm> #include <memory> struct Security; struct Bond; struct Stock; struct Checker { virtual bool check(const Security&) const { return false; }; virtual bool check(const Bond&) const { return false; } virtual bool check(const Stock&) const { return false; } }; struct Security { //virtual const char * getName() const = 0; virtual bool check(const Checker& checker) const = 0; }; struct Bond : public Security { //const char * getName() const { return "bond"; } bool check(const Checker& checker) const { return checker.check(*this); } }; struct Stock : public Security { //const char * getName() const { return "stock"; } bool check(const Checker& checker) const { return checker.check(*this); } }; struct IsBond : public Checker { bool check(const Bond&) const { return true; } bool check(const Security&) const { return false; } }; struct IsStock : public Checker { bool check(const Stock&) const { return true; } bool check(const Security&) const { return false; } }; template<class Checker> struct CheckerFunctor { bool operator()(const std::shared_ptr<Security> sec) const { static Checker checker; return sec->check(checker); } }; int main() { std::vector<std::shared_ptr<Security>> securities; securities.push_back(std::make_shared<Bond>()); securities.push_back(std::make_shared<Stock>()); securities.push_back(std::make_shared<Bond>()); securities.push_back(std::make_shared<Bond>()); securities.push_back(std::make_shared<Stock>()); securities.push_back(std::make_shared<Bond>()); securities.push_back(std::make_shared<Stock>()); securities.push_back(std::make_shared<Bond>()); std::cout << securities.size() << std::endl; securities.erase(std::remove_if(securities.begin(), securities.end(), CheckerFunctor<IsStock>()), securities.end()); std::cout << securities.size() << std::endl; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 1.13 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 1.28 sec
fork mode
|
history
|
discussion
8 5