From 965508d9e187d589070c5cd1853cc035b51ea3cf Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 24 Apr 2024 21:46:32 +0300 Subject: [PATCH] Fix type_name for abstract classes. Fixes #172. --- include/boost/core/type_name.hpp | 5 +++-- test/type_name_test.cpp | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/boost/core/type_name.hpp b/include/boost/core/type_name.hpp index 773b5f6..c5cbbbd 100644 --- a/include/boost/core/type_name.hpp +++ b/include/boost/core/type_name.hpp @@ -103,7 +103,8 @@ inline std::string fix_typeid_name( char const* n ) } // class types can be incomplete -template std::string typeid_name_impl( int T::* ) +// but also abstract (T[1] doesn't form) +template std::string typeid_name_impl( int T::*, T(*)[1] ) { std::string r = fix_typeid_name( typeid(T[1]).name() ); return r.substr( 0, r.size() - 4 ); // remove ' [1]' suffix @@ -116,7 +117,7 @@ template std::string typeid_name_impl( ... ) template std::string typeid_name() { - return typeid_name_impl( 0 ); + return typeid_name_impl( 0, 0 ); } // template names diff --git a/test/type_name_test.cpp b/test/type_name_test.cpp index 8ef6677..08cc872 100644 --- a/test/type_name_test.cpp +++ b/test/type_name_test.cpp @@ -55,6 +55,13 @@ template struct X template struct Y; +class W +{ +public: + + virtual void f() = 0; +}; + enum E1 { e1 @@ -122,6 +129,7 @@ int main() TEST(A); TEST(B); TEST(C); + TEST(W); TEST(E1); @@ -143,6 +151,9 @@ int main() TEST(C&); TEST(C const&); + TEST(W const); + TEST(W&); + #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) TEST(B&&); @@ -156,6 +167,8 @@ int main() TEST(C*); TEST(C const* volatile*); + TEST(W volatile*); + TEST(void*); TEST(void const* volatile*); @@ -307,6 +320,8 @@ int main() TEST(std::pair); + TEST(std::pair); + TEST(std::pair); TEST(std::pair, void>); @@ -336,9 +351,11 @@ int main() TEST(X volatile&); TEST(X); + TEST(X); TEST(Y); TEST(Y); + TEST(Y); TEST(X, void>);