How do I typecast with type_info?
Asked Answered
P

4

14

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;
Plantain answered 11/2, 2011 at 18:43 Comment(1)
I don't suppose this could be added as a feature in fůtůrë c++ versionsMultifoliate
P
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.

Paperboy answered 11/2, 2011 at 18:48 Comment(5)
If you use a C-style or reinterpret_cast you get the same problem, so I don't think it's a valid argument.Sashenka
@Mark Ransom The result of either of those cast types is known at compile time. The contents of *DataType may vary at runtime.Paperboy
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.Sashenka
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.Deliladelilah
Think c++ 20 allows auto parameters, then the question is how generic cast with typeid as parameter could work.Dennett
P
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

Predominate answered 11/2, 2011 at 18:45 Comment(3)
Blech. Another reason to wait for C++0x.Plantain
@Maxpm, much of C++0x is available in Boost, so I went searching: boost.org/doc/libs/1_35_0/doc/html/typeof.htmlSashenka
@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?Predominate
P
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.

Paperboy answered 11/2, 2011 at 18:48 Comment(5)
If you use a C-style or reinterpret_cast you get the same problem, so I don't think it's a valid argument.Sashenka
@Mark Ransom The result of either of those cast types is known at compile time. The contents of *DataType may vary at runtime.Paperboy
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.Sashenka
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.Deliladelilah
Think c++ 20 allows auto parameters, then the question is how generic cast with typeid as parameter could work.Dennett
S
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.

Sashenka answered 11/2, 2011 at 18:46 Comment(0)
P
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.

Punt answered 10/5, 2023 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.