vendredi 31 mai 2019

How do I reinterpret_cast between any two types?

I would like to re-declare the type of a given variable, but unfortunately reinterpret_cast<> does not help here. This line:

reinterpret_cast<std::vector<double>>(std::string("Hello"));

results in the following compiler error:

invalid cast from type 'std::string {aka std::basic_string<char>}' to type 'std::vector<double>'

Is there a different, clean approach?

Notes:

  • I know that reinterpret_cast<> is not a proper conversion. I really do only want to re-declare the type here. Some code around this line will make sure it is only performed when appropriate
  • Why actually does this line above not work?
  • I am aware of this option, which I think is too messy: *reinterpret_cast(&OldType)

Aucun commentaire:

Enregistrer un commentaire