13

I've stored a pointer to a type_info object.

int MyVariable = 123;
const std::type_info* Datatype = &typeid(MyVariable);

How might I use this to typecast another variable to that type? I tried this, but it doesn't work:

std::cout << ((*Datatype)3.14) << std::endl;

Using the function form of typecasting doesn't work, either:

std::cout << (*Datatype(3.14)) << std::endl;
Maxpm
  • 24,113
  • 33
  • 111
  • 170

4 Answers4

19

Simply you cannot do that using type_info. Also, in your example DataType is not a type, it's a pointer to an object of type type_info. You cannot use it to cast. Casting requires type, not pointer or object!


In C++0x, you can do this however,

    int MyVariable = 123;

    cout << (decltype(MyVariable))3.14 << endl;

    cout << static_cast<decltype(MyVariable)>(3.14) << endl;

Output:

3
3

Online Demo: http://www.ideone.com/ViM2w

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Blech. Another reason to wait for C++0x. – Maxpm Feb 11 '11 at 18:55
  • @Maxpm, much of C++0x is available in Boost, so I went searching: http://www.boost.org/doc/libs/1_35_0/doc/html/typeof.html – Mark Ransom Feb 11 '11 at 21:16
  • @Mark Ransom : I think `Boost.Typeof` is not portable, or simply doesn't work with all compilers, as it uses compiler extension internally. See this topic : [Absence of typeof operator in C++03?](http://stackoverflow.com/questions/4533758/absence-of-typeof-operator-in-c03) – Nawaz Feb 11 '11 at 21:20
6

I don't think such casting can be done. Suppose you could do "dynamic" casting like this at runtime (not to mean dynamic_cast). Then if you used the result of the cast to call a function the compiler could no longer do type checking on the parameters and you could invoke a function call that doesn't actually exist.

Therefore it's not possible for this to work.

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
Mark B
  • 95,107
  • 10
  • 109
  • 188
  • If you use a C-style or reinterpret_cast you get the same problem, so I don't think it's a valid argument. – Mark Ransom Feb 11 '11 at 18:52
  • 1
    @Mark Ransom The result of either of those cast types is *known at compile time*. The contents of `*DataType` may vary at runtime. – Mark B Feb 11 '11 at 18:59
  • 2
    on that point I think we agree and I said as much in my answer. Where I disagree is that the problem of losing type checking would be a reason for this not to work. If you cast a `foo *` to a `bar *` and call a method on it, you can invoke a function call that doesn't actually exist - today, using the casts I mentioned. – Mark Ransom Feb 11 '11 at 19:44
  • While your conclusion may be correct, your logic of arriving there isn't. Methods are passed the `this` parameter, à la `foo(Foo* this, ...)` so the function would exist, but the `this` pointer could, hypothetically, point to a non-`Foo` object. – csl May 19 '15 at 16:25
4

Typecasting isn't a run-time process, it's a compile-time process at least for the type you're casting to. I don't think it can be done.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

If you're willing to use fragile, GCC-specific logic, take a look at the __dynamic_cast and __do_upcast methods. These approximately correspond to dynamic_cast<> and static_cast<> and can be used to cast void * pointers for which you have the std::type_info.

tgoodhart
  • 3,111
  • 26
  • 37