Hacker News new | past | comments | ask | show | jobs | submit login

If I've understood this correctly, you can more or less do this in C++ by using voldemort types.

Here's a nonsensical but demonstrative example program:

  #include <string>
  #include <iostream>
  
  auto polyret()
  {
      struct proof_of_concept
      {
          operator int() { return 4; }
          operator double() { return 1.5; }
          operator std::string() { return "I'm a string!"; }
      };
      return proof_of_concept();
  }
  
  int main()
  {
      int a = polyret();
      double b = polyret();
      std::string c = polyret();
      std::cout << "int: " << a << std::endl; //prints 4
      std::cout << "double: " << b << std::endl; //prints 1.5
      std::cout << "string: " << c << std::endl; //prints "I'm a string!"
  }



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: