Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

return the name of the class

106 views
Skip to first unread message

Lynn McGuire

unread,
Nov 11, 2015, 1:28:22 PM11/11/15
to
Is there a way to return the name of the class that is being compiled?

Thanks,
Lynn

Alf P. Steinbach

unread,
Nov 11, 2015, 1:45:36 PM11/11/15
to
On 11/11/2015 7:27 PM, Lynn McGuire wrote:
>
> Is there a way to return the name of the class that is being compiled?

Depends.

In a method (non-static member function) you can include "<typeinfo>"
and use "typeid(*this).name()", but whether it returns a cleartext name
depends on the compiler. With Visual C++ it does. With g++ you can use
g++ specific functionality to translate the mangled name that is
returned, to a normal cleartext name.

As an alternative, the "__func__" name may include a class name
depending on the implementation.


Cheers & hth.,

- Alf

Victor Bazarov

unread,
Nov 11, 2015, 9:52:11 PM11/11/15
to
On 11/11/2015 1:27 PM, Lynn McGuire wrote:
> Is there a way to return the name of the class that is being compiled?

Return where? From what? To what?

C++ has no standard "reflection" mechanism. Maybe your compiler has it,
RTFM.

V
--
I do not respond to top-posted replies, please don't ask

Vladislav Yaroslavlev

unread,
Nov 15, 2015, 4:11:53 PM11/15/15
to
There is no standardised way. Depends on your compiler.

Lynn McGuire

unread,
Nov 18, 2015, 3:23:55 PM11/18/15
to
Yup, bummer. One would think that the C++ standards committee would have this now.

Thanks,
Lynn

Victor Bazarov

unread,
Nov 20, 2015, 8:24:53 PM11/20/15
to
Why do you think you need this?

Öö Tiib

unread,
Nov 20, 2015, 9:24:49 PM11/20/15
to
On Saturday, 21 November 2015 03:24:53 UTC+2, Victor Bazarov wrote:
> On 11/18/2015 3:23 PM, Lynn McGuire wrote:
> > On 11/15/2015 3:11 PM, Vladislav Yaroslavlev wrote:
> >> On Wednesday, November 11, 2015 at 9:28:22 PM UTC+3, Lynn McGuire wrote:
> >>> Is there a way to return the name of the class that is being compiled?
> >>>
> >>> Thanks,
> >>> Lynn
> >>
> >> There is no standardised way. Depends on your compiler.
> >
> > Yup, bummer. One would think that the C++ standards committee would
> > have this now.
>
> Why do you think you need this?

Typically such things are used for testing, debugging, mocking,
synthesizing new types or for compile-time verifying that some
parallel class hierarchies match.

Victor Bazarov

unread,
Nov 21, 2015, 7:21:35 AM11/21/15
to
Well, yes, thank you. I wanted to know the actual reason for grievance
in Lynn McGuire's case, not start a general discussion on development tools.

Öö Tiib

unread,
Nov 21, 2015, 3:50:34 PM11/21/15
to
Ah, she likely was disappointed that basically all C++ implementations
have had such information available for more than decade but it is so
badly standardized. I myself have found Boost.TypeIndex library to be
most helpful for use-cases that I listed. However lot of people are
afraid of Boost ... so it is hard to suggest.

Lynn McGuire

unread,
Nov 21, 2015, 10:55:46 PM11/21/15
to
On 11/20/2015 7:24 PM, Victor Bazarov wrote:
> On 11/18/2015 3:23 PM, Lynn McGuire wrote:
>> On 11/15/2015 3:11 PM, Vladislav Yaroslavlev wrote:
>>> On Wednesday, November 11, 2015 at 9:28:22 PM UTC+3, Lynn McGuire wrote:
>>>> Is there a way to return the name of the class that is being compiled?
>>>>
>>>> Thanks,
>>>> Lynn
>>>
>>> There is no standardised way. Depends on your compiler.
>>
>> Yup, bummer. One would think that the C++ standards committee would
>> have this now.
>
> Why do you think you need this?
>
> V

We have different behavior based on the name of the class. We do have a
Class::className method in all of our classes, I was just wondering if
we could forgo that now. I forget why, but virtualization does not work
in this particular case other than to supply the name of the class.

Thanks,
Lynn

Lynn McGuire

unread,
Nov 21, 2015, 10:56:05 PM11/21/15
to
He.

Thanks,
Lynn

Ian Collins

unread,
Nov 21, 2015, 11:12:43 PM11/21/15
to
Lynn McGuire wrote:
>
> We have different behavior based on the name of the class. We do have a
> Class::className method in all of our classes, I was just wondering if
> we could forgo that now. I forget why, but virtualization does not work
> in this particular case other than to supply the name of the class.

How many classes do you have? I some code that uses the index in a
typelist to generate a unique numerical ID for each type.

--
Ian Collins

Jorgen Grahn

unread,
Nov 22, 2015, 5:52:47 AM11/22/15
to
On Sat, 2015-11-21, 嘱 Tiib wrote:
...
> I myself have found Boost.TypeIndex library to be
> most helpful for use-cases that I listed. However lot of people are
> afraid of Boost ... so it is hard to suggest.

This is a bit embarrassing: I still haven't really used any part of
Boost. But surely it's a very valid suggestion? People today have
access to it, and compilers which can cope with it. Unless
Boost.TypeIndex is very bleeding edge or very complicated, it seems
like a good choice if you need to do such things.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Lynn McGuire

unread,
Nov 22, 2015, 8:44:24 AM11/22/15
to
600 or so classes. I have a manually built solution for now, I was just
wondering about the future:

std::string RefiGroup::className ()
{
return "RefiGroup";
}

Thanks,
Lynn

Öö Tiib

unread,
Nov 22, 2015, 12:46:38 PM11/22/15
to
On Sunday, 22 November 2015 12:52:47 UTC+2, Jorgen Grahn wrote:
> On Sat, 2015-11-21, Öö (not 嘱) Tiib wrote:
> ...
> > I myself have found Boost.TypeIndex library to be
> > most helpful for use-cases that I listed. However lot of people are
> > afraid of Boost ... so it is hard to suggest.
>
> This is a bit embarrassing: I still haven't really used any part of
> Boost. But surely it's a very valid suggestion?

For people who have to write platform-agnostic tools it is basically
best available option. For people who want to squeeze most out of sole
platform it is perhaps possible to take a day and read documentation
about hints found in <typeinfo> header for that platform.

Most portable C++ frameworks also contain some features but these are
usually weaker or narrower.
For example Qt 'pObj->metaObject()->className()'. It is run-time call
(OP mentioned compile-time) and it works only with objects that are
instrumented as 'Q_OBJECT'.

> People today have
> access to it, and compilers which can cope with it.

Yes, it is free, well-tested and works on most widely used platforms.
Also it is header-only and light since most of it works compile-time.

> Unless Boost.TypeIndex is very bleeding edge or very complicated,
> it seems like a good choice if you need to do such things.

TypeIndex is pure meta-programming. Meta-programming in C++ is
controversial topic. Authors of it seem to love newest features of
language but they also compensate older compilers and fix defective
compilers with the very same meta-programming. So that source
code can be quite difficult to follow even for expert at places.

It is no problem if you use it correctly, your code will look
clean and tidy. All dirty tricks are done on their side.
However on case of defect in your code you may receive scary and
incomprehensible compiler errors. :)




Alf P. Steinbach

unread,
Nov 22, 2015, 6:41:00 PM11/22/15
to
I just coded up the following yet again.

This code works with MinGW g++ and Visual C++, yielding the same result
with both compilers. Since it works with g++ it presumably works also
with clang, at least if the check for __GNUC__ is appropriately modified
(if necessary). And since it works with Visual C++ it presumably also
works with the Intel compiler for Windows.

I'm pretty sure it won't work directly with IBM's compiler, and would be
interested if anyone could post code for that compiler.


<file "demangled.hpp">
#pragma once
// Copyright © 2015 Alf P. Steinbach. Boost license version 1.0.

#include <string>

#include <typeinfo>
#ifdef __GNUC__
# include <cxxabi.h>
#endif

namespace cppx {
using Byte_string = std::string;

#ifdef __GNUC__
inline
auto demangled( char const* const s )
-> Byte_string
{
//
https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html
int status;
char* const clean_name = abi::__cxa_demangle( s, 0, 0, &status );
auto result = Byte_string( status == 0? clean_name : s );
free( clean_name );
return result;
}
#else
inline
auto demangled( char const* const s )
-> Byte_string
{ return s; }
#endif
} // namespace cppx
</file>


<file "type_name.hpp">
#pragma once
// Copyright © 2015 Alf P. Steinbach. Boost license version 1.0.

#include "demangled.hpp"
#include <stdexcept>
#include <typeinfo>
#include <utility>

#include <iostream>
namespace cppx {
using std::move;
using Byte_string = std::string;

namespace detail {
inline
auto last_word_of( Byte_string s )
-> Byte_string
{
auto const i_last_space = s.rfind( ' ' );
if( i_last_space != Byte_string::npos )
{
return s.substr( i_last_space + 1 );
}
return move( s );
}
}

template< class Type >
auto type_name()
-> Byte_string
{ return detail::last_word_of( demangled( typeid( Type ).name() ) ); }

template< class Type >
auto type_name( Type&& o )
-> Byte_string
{ return detail::last_word_of( demangled( typeid( o ).name() ) ); }
} // namespace cppx
</file>


<file "main.cpp">
#include "type_name.hpp"

#include <iostream>
#include <typeinfo>
using namespace std;

struct My_base { virtual ~My_base(){} };
struct My_type: My_base {};

enum My_enum {};

auto main() -> int
{
My_base&& o = My_type();
cout << cppx::type_name<int>() << endl;
cout << cppx::type_name<My_enum>() << endl;
cout << cppx::type_name<My_type>() << endl;
cout << cppx::type_name( o ) << endl;
}
</file>

Alf P. Steinbach

unread,
Nov 23, 2015, 6:10:51 AM11/23/15
to
On 11/23/2015 11:19 AM, Stefan Ram wrote:
> "Alf P. Steinbach" <alf.p.stein...@gmail.com> writes:
>> -> Byte_string
>> {
>> //
>> https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html
>> int status;
>> char* const clean_name = abi::__cxa_demangle( s, 0, 0, &status );
>> auto result = Byte_string( status == 0? clean_name : s );
>
> The label defined above actually is not used in the code as
> far as I can see. So the code fragment can be simplified to:
>
> -> Byte_string
> {
> //
> //gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html
> int status;
> char* const clean_name = abi::__cxa_demangle( s, 0, 0, &status );
> auto result = Byte_string( status == 0? clean_name : s );
>

:) Well spotted.

Or alternatively you were a victim of incorrect line wrapping. :/

My posting quoted above was posted with

Content-Type: text/plain; charset=koi8-r; format=flowed

as was your posting that I replied to, and at least the version I see is
correctly formatted. With flowed format a space at the end of physical
posted line, denotes a line continuation.

* * *

There are a number of C++ constructs where formatting can make it look
like something else. I can only recall two at the moment:

http://www.google.com; URL as valid statement
--> The "goes to" operator

The "goes to" operator was introduced by Andrew Koenig in this group,
many years ago, in connection with the problem of using unsigned loop
counters, IIRC.


Cheers!,

- Alf

0 new messages