Boost logo

Boost :

Subject: Re: [boost] [TypeIndex] Peer review period for library acceptance begins, ending Thurs 21st Nov
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2013-11-14 07:27:48


On Thu, Nov 14, 2013 at 4:08 PM, Antony Polukhin <antoshkka_at_[hidden]> wrote:
> 2013/11/14 Andrey Semashev <andrey.semashev_at_[hidden]>
>
>> I think that these problems can be tackled by just having
>> boost::type_index and boost::type_id. boost::type_index can implement
>> the name functions we discussed and offer interfacing with
>> std::type_info and implement workarounds for its bugs. boost::type_id
>> (or preferably its variations) can solve problems with
>> cv-qualification, references and visibility by wrapping the type
>> before constructing boost::type_index.
>>
>> The only reason for boost::type_info I see is to emulate RTTI when
>> it's disabled. But as I see in the code, it is just an alias for
>> template_info in this case. So perhaps we should just make it that -
>> an alias to std::type_info or boost::template_info, depending on the
>> configuration. This way it will just allow to use the type_info type
>> portably, without preprocessor checks everywhere.
>
>
> We are arguing about
>
> class type_info: public std::type_info { ...};
>
> vs
>
> class type_info{
> const std::type_info* ptr_;
> public:
> const std::type_info& get();
> ...
> };
> typedef ... std_type_info_if_exist_t;
>
> Can you explain me once more, what are the advantages of the second
> solution?

No, I was thinking about something like this:

// type_index/type_info.hpp

namespace boost {

#if HAS_RTTI
typedef std::type_info type_info;
#else
typedef boost::template_info type_info;
#endif

}

// type_index/type_index.hpp
#include <boost/type_index/type_info.hpp>

namespace boost {

class type_index
{
  boost::type_info const* m_type_info;

public:
  explicit type_index(boost::type_info const&);
  boost::type_info const& get() const;
  // as previously discussed:
  const char* name() const;
  const char* raw_name() const;
  std::string pretty_name() const;
};

#if HAS_RTTI
template< typename T >
type_index type_id()
{
  return type_index(typeid(T));
}
#else
template< typename T >
type_index type_id()
{
  return type_index(template_info::construct<T>());
}
#endif

}

// any.hpp
#include <boost/type_index/type_index.hpp>

class any
{
  type_index m_type;

public:
  boost::type_info const& type() const { return m_type.get(); }
};

The benefit of this approach is that you don't have to write hacks on
top of std::type_info and still get portable code (with and without
native RTTI).


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