Boost logo

Boost :

Subject: Re: [boost] Is there interest in typeid(TYPE).name() like function that does not require RTTI
From: Michel Morin (mimomorin_at_[hidden])
Date: 2012-05-29 18:58:41


Antony Polukhin wrote:
> Would be there some interest in function for getting class name without RTTI?

Boost.Exception has such a function boost::type_name in
<boost/exception/detail/type_info.hpp>.

Personally, I'm using the following code.
This code is useful because it does not strip off cv- or ref-qualifiers
(e.g. boost::type_name<int const&>() --> "int const&").

#include <string>
#include <boost/config.hpp>
#ifndef BOOST_NO_TYPEID
#include <boost/units/detail/utility.hpp>
#else
#include <boost/current_function.hpp>
#endif

namespace boost {
namespace type_name_detail {
#ifndef BOOST_NO_TYPEID
template <typename T>
struct demangle_impl
{
    static std::string apply()
    {
        return boost::units::detail::demangle(typeid(T).name());
    }
};

template <typename T>
struct demangle_impl<T const>
{
    static std::string apply()
    {
        return demangle_impl<T>::apply() + " const";
    }
};

template <typename T>
struct demangle_impl<T volatile>
{
    static std::string apply()
    {
        return demangle_impl<T>::apply() + " volatile";
    }
};

template <typename T>
struct demangle_impl<T const volatile>
{
    static std::string apply()
    {
        return demangle_impl<T>::apply() + " const volatile";
    }
};

template <typename T>
struct demangle_impl<T&>
{
    static std::string apply()
    {
        return demangle_impl<T>::apply() + "&";
    }
};

#ifndef BOOST_NO_RVALUE_REFERENCES
template <typename T>
struct demangle_impl<T&&>
{
    static std::string apply()
    {
        return demangle_impl<T>::apply() + "&&";
    }
};
#endif
#endif

} // namespace type_name_detail

template <typename T>
inline std::string type_name()
{
#ifndef BOOST_NO_TYPEID
    return type_name_detail::demangle_impl<T>::apply();
#else
    return BOOST_CURRENT_FUNCTION;
#endif
}

} // namespace boost

Regards,
Michel


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