Hi, any is really a powerful class, but sometimes I would also be able to know directly the type stored into it without 'guessing', for example to use it in template code. What I would like to have is something as type_info::real_type for the value hidden into any (not legal C++, maybe legal C++0x ?), or, alternatively, virtual template functions (again not legal C++) to be able to write generic code depending on both hidden value's type and types 'from the outside world' (sorry cannot explain it better :-( than this) I have tried various approaches, but if I 'visit' the any class adding a virtual function to reach hidden value I loose generic type information (lack of virtual template functions) from the outside while from the outside of any I cannot obtain directly the type of the hidden value ... is there a way to do the above ? Gianni Luciani
On Thursday 08 November 2001 20:07, you wrote:
Hi,
any is really a powerful class, but sometimes I would also be able to know directly the type stored into it without 'guessing', for example to use it in template code. What I would like to have is something as
type_info::real_type for the value hidden into any (not legal C++, maybe legal C++0x ?),
or, alternatively, virtual template functions (again not legal C++) to be able to write generic code depending on both hidden value's type and types 'from the outside world' (sorry cannot explain it better :-( than this)
I have tried various approaches, but if I 'visit' the any class adding a virtual function to reach hidden value I loose generic type information (lack of virtual template functions) from the outside while from the outside of any I cannot obtain directly the type of the hidden value ...
is there a way to do the above ?
Short answer: no. Asking for the type given only a type_info object is asking a runtime entity (the object returned by typeid) to return a compile-time entity (a type). To make a scheme such as this work for all types (without enumerating them) would require all information about types to be resolved at compile time. At that point, static checking of types within C++ becomes impossible, and C++ starts to feel like Smalltalk <shutters>. Doug
On Thu, 8 Nov 2001, Douglas Gregor wrote:
On Thursday 08 November 2001 20:07, you wrote: [ SNIP ]
type_info::real_type for the value hidden into any (not legal C++, maybe legal C++0x ?), [ SNIP ] is there a way to do the above ?
Short answer: no.
Asking for the type given only a type_info object is asking a runtime entity (the object returned by typeid) to return a compile-time entity (a type). To make a scheme such as this work for all types (without enumerating them) would require all information about types to be resolved at compile time. At that point, static checking of types within C++ becomes impossible, and C++ starts to feel like Smalltalk <shutters>.
Hmm, then the other approach: are there ways to simulate virtual template functions ? Regards Gianni Luciani
participants (2)
-
Douglas Gregor
-
Gianni Luciani