
Show HN: Nameof Operator for Modern C++ - neargye
https://github.com/Neargye/nameof
======
koyote
This is a good use of the more modern C++ features such as string_view,
constexpr and template metafunctions such as decay and enable_if.

I do wonder how much this would impact compile-times if used moderately in a
large code-base.

~~~
neargye
In our project, NAMEOF_TYPE and NAMEOF almost do not add time relative to the
entire project. But NAMEOF_ENUM can add a couple of seconds to large enums.

------
CalChris
This is similar to the C preprocessor trick _STRINGIZE_ :

    
    
      #define STRINGIZE_DETAIL(x) #x
      #define STRINGIZE(x)        STRINGIZE_DETAIL(x)

~~~
gpderetta
It probably uses stringification under the hood, but then relies on constexpr
to "canonicalize" the name (removing everything before the dot, template
parameters etc).

~~~
Someone
From cursory reading, it seems it doesn’t, at all. It uses compiler-specific
predefined values __PRETTY_FUNCTION__ (gcc, clang) or __FUNCSIG__ (MS Visual
C++), and doesn’t work on other compilers.

It also has to do work to extract the function name from the char arrays/C
strings that those macros expand to.

Reading answers to [https://stackoverflow.com/questions/4384765/whats-the-
differ...](https://stackoverflow.com/questions/4384765/whats-the-difference-
between-pretty-function-function-func/26907887), it seems it would have been
easier to use the __FUNCTION__ in gcc, but I’m not familiar with that (or the
other predefined values) at all, so who knows?

~~~
anon4242
Actually, __FUNCTION__ won't work as it _only_ expands to the name of the
function, not any templates or arguments.

It's actually a pretty clever way to automagically deduce not only names of
variables but also of enums :). Too bad it's most likely not maintainable in
the long run as compilers probably change what their __PRETTY_FUNCTION__ (or
equivalent) macros expand to in new versions.

~~~
neargye
I usually check the newest versions of compilers, and if the behavior changes,
then I update the code

------
gumby
Nice work, and the header is nowhere near as complex as I had feared it would
need to be.

~~~
neargye
With features C++17 this header looks not so terribly.

------
shhehebehdh
I wonder what including and using this does to compile times. Nothing good, I
suspect.

~~~
neargye
In fact, not so much about the whole big project. May add a few seconds to the
compilation.

------
jhasse
nameof_enum doesn't work with GCC :(

~~~
neargye
New version supported GCC >= 9

~~~
jhasse
Awesome :)

