0

Say I have a class like this:

class Instruction {
  std::string name;
  enum valtype { STRING, INSTRUCTION };
  Instruction* value_i;
  std::string value_s;
}

Is there any way to just make one variable and have it's type specified at runtime? I have looked at unions, but they can't have things like strings in them. I would prefer C++98.

Finxx
  • 61
  • 1
  • 7
  • 3
    The earliest useful C++ version is C++11. Why would you want to use C++98? In C++17 what you want is [std::variant](https://en.cppreference.com/w/cpp/utility/variant) which is exactly a tagged union, and in some rare cases [std::any](https://en.cppreference.com/w/cpp/utility/any) – phuclv Jun 12 '22 at 02:26
  • because I am using an old computer that's compiler only supports up to c++98. Modern solutions would be fine, but I would prefer there to be a c++98 solution. – Finxx Jun 12 '22 at 02:29
  • Getting a C++11 compiler working would be a massive pain, but if it is the _only_ option, then I could try. – Finxx Jun 12 '22 at 02:33
  • 1
    nowadays you should try at least C++17 or better C++20, they contain lots of much more useful features. Are you working on some ancient hardware? – phuclv Jun 12 '22 at 02:45
  • @Finxx if you are stuck using C++98, then a `union` can work, but as you noted it can't hold a `std::string` (that ability was added in C++11) unless you allocate it dynamically at runtime and store a `string*` pointer instead. Otherwise, use a `char[]` array instead. – Remy Lebeau Jun 12 '22 at 02:47
  • While I agree that trying to use more recent versions of C++ is a good idea, you might be able to find alternatives on github, for example [this](https://github.com/martinmoene/variant-lite) repo claims to have a c++98 variant-like type which seems to be what youre looking for. – Borgleader Jun 12 '22 at 02:48
  • Short answer: nope. C++ does not work this way. In C++17 you have `std::variant`, that's probably as close as you can get, here. – Sam Varshavchik Jun 12 '22 at 02:48
  • I am using a PowerPC Mac G4 and Xcode 2.0 (2005), so you could say it is ancient. I will take a look at that GitHub repo mentioned. – Finxx Jun 12 '22 at 02:52
  • @Finxx you can install latest Linux and latest compilers on PPC. It's already quite modern, unlike DOS and other retrocomputing things – phuclv Jun 12 '22 at 03:59
  • If you can't use `std::variant` then you can just use `boost::variant` which is what the standard library version was based on – Alan Birtles Jun 12 '22 at 05:47
  • `reinterpret_cast` was part of C++98 too. Along with a `char data[sizeof(what)];`, everything can be done. Robustness, reliability, readability are problems hard to solve using old tech. – Red.Wave Jun 12 '22 at 09:35
  • @Red.Wave [using `reinterpret_cast` for type punning invokes UB](https://stackoverflow.com/a/54017015/995714) so it's not a solution either – phuclv Jun 12 '22 at 16:15
  • @phuclv the OP is asking for `union`; not any better. Implementation of `std::variant` is also either of the above. – Red.Wave Jun 12 '22 at 18:28
  • @Red.Wave who said that? Definitely no one uses `reinterpret_cast` for type punning, and I'm not talking anything about union, implementations can do anything as long as it's allowed by the standard, and they may very likely use compiler intrinsics for better performance than C++ tricks on union – phuclv Jun 13 '22 at 01:44

0 Answers0