Boost logo

Boost :

Subject: [boost]  Is there interest in typeid(TYPE).name() like function that does not require RTTI
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2012-05-29 15:29:02


Hi,

Would be there some interest in function for getting class name without RTTI?

Such function can be implemented like that:

template <class TemplateNameT>
std::string template_type(const TemplateNameT& v) {
#ifdef _MSC_VER
   const char* const template_name_begin =
strstr(BOOST_CURRENT_FUNCTION, "(const ") + sizeof("(const ") - 1;
   BOOST_ASSERT(template_name_begin);

   const char* const template_name_end = template_name_begin +
strlen(template_name_begin) - sizeof("&)");
#else
   // Code for other platforms
#endif

   return std::string(template_name_begin, template_name_end);
}

If there is some interest, I'll update code to work on other
platforms, and change method signature from template_type(instance) to
template_type<InstanceTypeT>::name()

Usage example:

// Function can be compiled with RTTI disabled.
template <class T>
void foo(const T& val) {
   std::cerr << template_type<T>::name(); // instead of typeid(T).name();
}

--
Best regards,
Antony Polukhin

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