nameof icon indicating copy to clipboard operation
nameof copied to clipboard

type name without struct/class/enum

Open Ubpa opened this issue 4 years ago • 12 comments

struct A{};
class B{};
enum C{};
template<typename T>
class D{};
constexpr auto name_A = nameof::nameof_type<A>(); // "struct A"
constexpr auto name_B = nameof::nameof_type<B>(); // "class B"
constexpr auto name_C = nameof::nameof_type<C>(); // "enum C"
constexpr auto name_D_A = nameof::nameof_type<D<A>>(); // "class D<struct A>"

but I want

  • name_A == "A"
  • name_B == "B"
  • name_C == "C"
  • name_D_A == D<A>

Is it possible to get them in compile-time?

Ubpa avatar Aug 06 '20 14:08 Ubpa

In theory, this is possible. But I'm not sure if implement this at the compile stage.

Neargye avatar Aug 06 '20 15:08 Neargye

In gcc and clang, the results have no class-key (struct, class and enum). For some cross-compiler program using nameof, it's necessary to keep results same in different compilers. So I think this feature is necessary. I has pull a request, and add a new function nameof::nameof_brief_type. If you want to add this feature, please merge it and you could write some tests and doc for it later. Maybe the word brief can be substituteed by other words, e.g. cross_platform, cross, x, X.

Ubpa avatar Aug 07 '20 04:08 Ubpa

Also gcc, clang and msvc different process anonymous namespace

Neargye avatar Aug 07 '20 07:08 Neargye

"please merge it and you could write some tests and doc for it later" <-- something not right about that order. ;o)

ScottHutchinson avatar Aug 07 '20 15:08 ScottHutchinson

yeah, it should be tested first

Ubpa avatar Aug 08 '20 02:08 Ubpa

@Ubpa I now look at the difference in the behavior of different compilers, and I would not do a separate function and would update the current behavior nameof_type

Neargye avatar Aug 09 '20 09:08 Neargye

If you look at the different outputs of the compilers.

  • msvc class 'anonymous namespace'::A<int const > const &

    • remove struct_/class_/enum_
    • 'anonymous namespace' -> {anonymous namespace}
    • _& -> &
    • _* -> *
  • clang const {anonymous namespace}::A<const int> &

    • _& -> &
    • _* -> *
  • gcc const (anonymous namespace)::A<const int>&

    • (anonymous namespace) -> {anonymous namespace}
  • Const-Volatile normalize

I'm not sure if I have listed all the differences.

Neargye avatar Aug 10 '20 06:08 Neargye

If the name is end with const / volatile / const volatile, then move it to the beginning of the name. Then run the function recursively for every substrings within < and >. Do you think it's a reasonable proposal?

Ubpa avatar Aug 12 '20 13:08 Ubpa

@Ubpa Sounds good. The main thing is that it does not take too much time at the compilation.

Neargye avatar Aug 12 '20 17:08 Neargye

Would you give it a try? Or do you need to see my implementation first? Maybe I can try it when I'm free.

Ubpa avatar Aug 14 '20 08:08 Ubpa

I'll deal with this later, I have other projects in the pipeline. If you have a desire to do this, I would be happy to.

Neargye avatar Aug 14 '20 09:08 Neargye

I make it by another way. The name of type is same in different compiler.

Name.h

example

Ubpa avatar Dec 11 '20 18:12 Ubpa