fork download
  1. #include <vector>
  2. #include <string>
  3. #include <functional>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. struct Object {
  9. template <typename T>
  10. Object(T t) : model(new Model<T>(t)) {
  11. cout << "construct object from T" << endl;
  12. }
  13. friend void print(string str) {
  14. cout << str << endl;
  15. }
  16. friend void print(int i) {
  17. cout << i << endl;
  18. }
  19. struct Concept {
  20. virtual void print() =0;
  21. };
  22. template <typename T>
  23. struct Model : Concept {
  24. Model(T t) : data(t) {}
  25. void print() {
  26. print(data);
  27. }
  28. T data;
  29. };
  30. Concept *model;
  31. };
  32.  
  33. struct For {
  34. For() {
  35. cout << "default construct For" << endl;
  36. }
  37. For(Object o) {
  38. cout << "construct For from object and push back" << endl;
  39. objects.push_back(o);
  40. }
  41. For operator()(Object o) {
  42. cout << "push back object" << endl;
  43. objects.push_back(o);
  44. return *this;
  45. }
  46. void print() {
  47. for(auto o : objects) {
  48. o.model->print();
  49. }
  50. };
  51. vector<Object> objects;
  52. };
  53.  
  54. int main() {
  55. auto heterogeneous = For(1)(string("hello"));
  56. function<void(int)> f = [](int i)->void{ cout << i << endl; };
  57. heterogeneous.print();
  58. }
  59.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘void Object::Model<T>::print() [with T = std::__cxx11::basic_string<char>]’:
prog.cpp:58:5:   required from here
prog.cpp:26:22: error: no matching function for call to ‘Object::Model<std::__cxx11::basic_string<char> >::print(std::__cxx11::basic_string<char>&)’
                 print(data);
                 ~~~~~^~~~~~
prog.cpp:25:18: note: candidate: void Object::Model<T>::print() [with T = std::__cxx11::basic_string<char>]
             void print() {
                  ^~~~~
prog.cpp:25:18: note:   candidate expects 0 arguments, 1 provided
prog.cpp: In instantiation of ‘void Object::Model<T>::print() [with T = int]’:
prog.cpp:58:5:   required from here
prog.cpp:26:22: error: no matching function for call to ‘Object::Model<int>::print(int&)’
                 print(data);
                 ~~~~~^~~~~~
prog.cpp:25:18: note: candidate: void Object::Model<T>::print() [with T = int]
             void print() {
                  ^~~~~
prog.cpp:25:18: note:   candidate expects 0 arguments, 1 provided
stdout
Standard output is empty