Dmitrii
Dmitrii

Reputation: 637

Return alias identificator instead of type in typeid(SOME_TYPE).name()

In the following example

template <typename T>
void foo() {
    const char* name = typeid(T).name();
    std::cout << name;
}

the variable 'name' will be initialized by type 'T' name. It is very convinient if we need to print this template type name. But, if we have the alias:

using shortTypeName = std::smth::smth_else::smth_more;

in the result of the call

foo<shortTypeName>();

will be printed 'std::smth::smth_else::smth_more'. And I need to print exactly the alias name, but not the type it is defined. Can somebody give an advice, how I can do this?

Upvotes: 1

Views: 872

Answers (3)

Useless
Useless

Reputation: 67713

... alias identificator ...

There's no such thing, at least not after compilation. It's just syntactic sugar and doesn't exist in any sense in the final executable.

The only way to grab the local name of a type (as opposed to the implementation-defined and probably-mangled typeid name) is with a stringize/stringify macro.


For future reference, this should eventually be possible when the Reflection TS lands - but I don't yet know whether to expect that to look like reflexpr(T).get_name(), or std::meta::name_of(^T), or something else again.

Upvotes: 2

bolov
bolov

Reputation: 75668

You can't. Because a type alias is transparent: It is just a synonym for a new type, not a new type. As an implementation detail it doesn't even get mangled in the type system because it's not a type.

§7.1.3 The typedef specifier [dcl.typedef]

  1. [...] A name declared with the typedef specifier becomes a typedef-name . Within the scope of its declaration, a typedef-name is syntactically equivalent to a keyword and names the type associated with the identifier in the way described in Clause 8. A typedef-name is thus a synonym for another type. A typedef-name does not introduce a new type the way a class declaration (9.1) or enum declaration does.

  2. A typedef-name can also be introduced by an alias-declaration . The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name . It has the same semantics as if it were introduced by the typedef specifier. In particular, it does not define a new type and it shall not appear in the type-id .

typeid(T).name(); is pretty much useless anyway. Until we have proper introspection in C++ you have to resort to hacks to get what you want (macros, intrusive techniques or external code generator tools).

Upvotes: 1

R Sahu
R Sahu

Reputation: 206557

Can somebody give an advice, how I can do this?

The language does not support a mechanism to do this.

What you have is simply a type alias.

From http://en.cppreference.com/w/cpp/language/type_alias:

A type alias declaration introduces a name which can be used as a synonym for the type denoted by type-id. It does not introduce a new type and it cannot change the meaning of an existing type name.

Upvotes: 1

Related Questions