fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Tensor
  5. {
  6. public:
  7. Tensor& setA() { cout << "A"; return *this; }
  8. Tensor& setB() { cout << "B"; return *this; }
  9. Tensor& setC() { cout << "C"; return *this; }
  10. Tensor& setD() { cout << "D"; return *this; }
  11. Tensor& setE() { cout << "E"; return *this; }
  12. void build() { cout << endl; }
  13. };
  14.  
  15. Tensor& doSetE(Tensor &t)
  16. {
  17. return t.setE();
  18. }
  19.  
  20. Tensor& doNoOp(Tensor &t)
  21. {
  22. return t;
  23. }
  24.  
  25. void doTest(bool flag)
  26. {
  27. Tensor& (*temp)(Tensor&) = flag ? &doSetE : &doNoOp;
  28.  
  29. temp(Tensor{}
  30. .setA()
  31. .setB()
  32. .setC()
  33. .setD())
  34. .build();
  35. }
  36.  
  37. int main()
  38. {
  39. doTest(false);
  40. doTest(true);
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5372KB
stdin
Standard input is empty
stdout
ABCD
ABCDE