fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <tr1/functional>
  4.  
  5. class foo {
  6. private:
  7. int val;
  8. foo() {}
  9. foo(int v) : val(v) {}
  10. public:
  11. static foo create(int v) { return foo(v); }
  12. int value() const { return val; }
  13. };
  14.  
  15. class bar {
  16. private:
  17. int val;
  18. bar(const bar&) {}
  19. public:
  20. bar() {}
  21. bar(int v) : val(v) {}
  22. int value() const { return val; }
  23. void value(int v) { val = v; }
  24. };
  25.  
  26. class baz {
  27. private:
  28. int val;
  29. baz() {}
  30. baz(int v) : val(v) {}
  31. baz(const baz&) {}
  32. public:
  33. int value() const { return val; }
  34. static baz create(int v) { return baz(v); }
  35. };
  36.  
  37. int main()
  38. {
  39. using namespace std;
  40. {
  41. vector<foo> v;
  42. v.push_back(foo::create(1));
  43. cout << v[0].value() << endl;
  44. }
  45.  
  46. {
  47. vector<tr1::reference_wrapper<bar> > v;
  48. bar x(2);
  49. v.push_back(tr1::ref(x));
  50. bar& y = v[0];
  51. cout << v[0].get().value();
  52. y.value(3);
  53. cout << ' ' << v[0].get().value() << ' ' << x.value() << endl;
  54. }
  55.  
  56. {
  57. vector<tr1::reference_wrapper<const baz> > v;
  58. const baz& x = baz::create(4);
  59. v.push_back(tr1::ref(x));
  60. const baz& y = v[0];
  61. cout << y.value() << ' ' << v[0].get().value() << endl;
  62. }
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0s 2860KB
stdin
Standard input is empty
stdout
1
2 3 3
4 4