Boost logo

Boost :

From: remi.chateauneu_at_[hidden]
Date: 2007-05-15 12:03:22


Larry Evans a écrit :
>> ... generating a container of :
>> std::pair< const std::type_info *, boost::any >
>>
> But boost::any::type() returns std::type_info const&:
>
> http://www.boost.org/doc/html/boost/any.html#id717459-bb
>
> wouldn't that make the above pair contain redundant information?
>
In this specific case, yes (Using type_info & as a key is mandatory only
when 'assembling' different properties types at run-time) . Now, imagine
that you wish to apply another operation than 'any' to all members of an
object, the first example to jumps to my mind is the transformation into
a XML tag. You would have something like that:

  template< typename Struct, typename Type, typename Struct::*Member >
  struct ToXml {
  static std::string make_tag( const Struct & anObj ) {
    return "<" + std::string( typeid(Member) + ">" + to_string(
anObj->*Member ) + "</" + std::string( typeid(Member) + ">" ;
  };

Now, you can put these objects in the same list because they have the
same type:

std::pair< const std::type_info *, &ToXml< myStruct, Type1, Struct::Field1 >::make_tag >

std::pair< const std::type_info *, &ToXml< myStruct, Type2, Struct::Field2 >::make_tag >

With such a list, plus an object of type Struct, you can apply 'toXml' to all members of the object. And if you need a very specific processing for such or such field, you just need to replace an element in the list. But crd creates the initial list with the compile-time definitions of members (A kind of boost::mpl::tuple ) plus the definition of 'ToXml'.

 

>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk