nameof
nameof copied to clipboard
type name without struct/class/enum
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?
In theory, this is possible. But I'm not sure if implement this at the compile stage.
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
.
Also gcc, clang and msvc different process anonymous namespace
"please merge it and you could write some tests and doc for it later" <-- something not right about that order. ;o)
yeah, it should be tested first
@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
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}
-
_&
->&
-
_*
->*
- remove
-
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.
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 Sounds good. The main thing is that it does not take too much time at the compilation.
Would you give it a try? Or do you need to see my implementation first? Maybe I can try it when I'm free.
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.