fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #define P(x) std::cout<<x<<std::endl
  5.  
  6. struct Variable { void test() { P("variable"); } };
  7. struct Scalar : public Variable { void test() { P("scalar"); } };
  8. struct Array : public Variable { void test() { P("array"); } };
  9.  
  10. Variable && get(char query)
  11. {
  12. switch(query)
  13. {
  14. case '$': return std::move(Scalar());
  15. case '@': return std::move(Array());
  16.  
  17. default: throw 1;
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. Scalar a = (Scalar&&)get('$');
  24. Array b = (Array&&)get('@');
  25.  
  26. a.test();
  27. b.test();
  28. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
scalar
array