fork download
  1. #include <memory>
  2. #include <unordered_map>
  3. #include <iostream>
  4. #include <functional>
  5. #include <string>
  6.  
  7. class Base {};
  8.  
  9. class Factory {
  10. public:
  11. typedef std::unordered_map<std::string, std::function<std::shared_ptr<Base>(void)>> map_type; // wow... lol
  12.  
  13. template<typename T>
  14. static std::shared_ptr<Base> createInstance() {
  15. return std::shared_ptr<Base>(new T{});
  16. }
  17.  
  18. static std::shared_ptr<map_type> getMap() {
  19. if (map_ == nullptr) {
  20. map_.reset(new map_type);
  21. }
  22. return map_;
  23. }
  24.  
  25. private:
  26. static std::shared_ptr<map_type> map_;
  27. };
  28.  
  29. std::shared_ptr<Factory::map_type> Factory::map_ = nullptr;
  30.  
  31. template<typename T>
  32. class RegisterPlaceholder : Factory {
  33. public:
  34. RegisterPlaceholder(std::string const& s) {
  35. Factory::getMap()->insert(std::make_pair(s, &createInstance<T>));
  36. }
  37. };
  38.  
  39. class Derived1 : public Base {
  40. public:
  41. Derived1() {
  42. std::cout << "Derived1" << std::endl;
  43. }
  44. static RegisterPlaceholder<Derived1> reg_;
  45. };
  46. class Derived2 : public Base {
  47. public:
  48. Derived2() {
  49. std::cout << "Derived2" << std::endl;
  50. }
  51. static RegisterPlaceholder<Derived2> reg_;
  52. };
  53.  
  54. RegisterPlaceholder<Derived1> Derived1::reg_{"Derived1"};
  55. RegisterPlaceholder<Derived2> Derived2::reg_{"Derived2"};
  56.  
  57. int
  58. main() {
  59. auto& map = *Factory::getMap();
  60. map["Derived1"]();
  61. map["Derived2"]();
  62. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/unordered_map:40,
                 from prog.cpp:2:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/c++0x_warning.h:36:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/unordered_map:48,
                 from prog.cpp:2:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/type_traits:82: error: template argument 1 is invalid
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/type_traits:106: error: template argument 1 is invalid
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/type_traits:136: error: expected unqualified-id before ‘&&’ token
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/hashtable.h:55,
                 from /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/unordered_map:54,
                 from prog.cpp:2:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable:223: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable:223: error: invalid constructor; you probably meant ‘std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys> (const std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>&)’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable:232: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable:660: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable:660: error: prototype for ‘std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hashtable(std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>)’ does not match any in class ‘std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable:619: error: candidates are: std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hashtable(const std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>&)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable:218: error:                 template<class _Key, class _Value, class _Allocator, class _ExtractKey, class _Equal, class _H1, class _H2, class _Hash, class _RehashPolicy, bool __cache_hash_code, bool __constant_iterators, bool __unique_keys> template<class _InputIterator> std::_Hashtable::_Hashtable(_InputIterator, _InputIterator, typename _Allocator::size_type, const _H1&, const _H2&, const _Hash&, const _Equal&, const _ExtractKey&, const _Allocator&)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable:557: error:                 std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hashtable(typename _Allocator::size_type, const _H1&, const _H2&, const _Hash&, const _Equal&, const _ExtractKey&, const _Allocator&)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable:714: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable: In member function ‘void std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::swap(std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>)’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/hashtable:723: error: ‘__x’ was not declared in this scope
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/unordered_map:69,
                 from prog.cpp:2:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: At global scope:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:90: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:90: error: invalid constructor; you probably meant ‘std::__unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code> (const std::__unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code>&)’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:147: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:147: error: invalid constructor; you probably meant ‘std::__unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code> (const std::__unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code>&)’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:205: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:205: error: invalid constructor; you probably meant ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc> (const std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>&)’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:209: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: In member function ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator=(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>)’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:213: error: ‘__x’ was not declared in this scope
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: At global scope:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:254: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:254: error: invalid constructor; you probably meant ‘std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc> (const std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>&)’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:258: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: In member function ‘std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::operator=(std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>)’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:262: error: ‘__x’ was not declared in this scope
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: At global scope:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:283: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: In function ‘void std::swap(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>)’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:285: error: ‘__x’ was not declared in this scope
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:285: error: ‘__y’ was not declared in this scope
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: At global scope:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:290: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: In function ‘void std::swap(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>&, std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>)’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:291: error: ‘__y’ was not declared in this scope
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: At global scope:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:295: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: In function ‘void std::swap(std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>)’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:297: error: ‘__x’ was not declared in this scope
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:297: error: ‘__y’ was not declared in this scope
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: At global scope:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:302: error: expected ‘,’ or ‘...’ before ‘&&’ token
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map: In function ‘void std::swap(std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>&, std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>)’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/tr1_impl/unordered_map:303: error: ‘__y’ was not declared in this scope
prog.cpp: At global scope:
prog.cpp:11: error: ‘function’ is not a member of ‘std’
prog.cpp:11: error: ‘function’ is not a member of ‘std’
prog.cpp:11: error: ‘shared_ptr’ is not a member of ‘std’
prog.cpp:11: error: template argument 2 is invalid
prog.cpp:11: error: template argument 5 is invalid
prog.cpp:11: error: expected unqualified-id before ‘void’
prog.cpp:11: error: expected `)' before ‘void’
prog.cpp:14: error: expected initializer before ‘<’ token
prog.cpp:18: error: ISO C++ forbids declaration of ‘shared_ptr’ with no type
prog.cpp:18: error: invalid use of ‘::’
prog.cpp:18: error: expected ‘;’ before ‘<’ token
prog.cpp:62: error: expected `;' at end of input
prog.cpp:62: error: expected `}' at end of input
prog.cpp:62: error: expected unqualified-id at end of input
stdout
Standard output is empty