News for hackers. Stuff that matters.
Re:Show HN: Nameof Operator for Modern C++ by koyote on Sunday March 24, 2019 @05:21PM (#19478850)
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.

Re:Show HN: Nameof Operator for Modern C++ by neargye on Monday March 25, 2019 @03:58AM (#19480914)
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.
Re:Show HN: Nameof Operator for Modern C++ by CalChris on Sunday March 24, 2019 @02:45PM (#19478002)
This is similar to the C preprocessor trick STRINGIZE:

  #define STRINGIZE_DETAIL(x) #x
  #define STRINGIZE(x)        STRINGIZE_DETAIL(x)
Re:Show HN: Nameof Operator for Modern C++ by gpderetta on Sunday March 24, 2019 @03:23PM (#19478218)
It probably uses stringification under the hood, but then relies on constexpr to "canonicalize" the name (removing everything before the dot, template parameters etc).
Re:Show HN: Nameof Operator for Modern C++ by Someone on Sunday March 24, 2019 @04:07PM (#19478465)
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..., 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?

Re:Show HN: Nameof Operator for Modern C++ by anon4242 on Sunday March 24, 2019 @04:54PM (#19478708)
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.

Re:Show HN: Nameof Operator for Modern C++ by neargye on Monday March 25, 2019 @04:07AM (#19480939)
I usually check the newest versions of compilers, and if the behavior changes, then I update the code
Re:Show HN: Nameof Operator for Modern C++ by asveikau on Sunday March 24, 2019 @05:05PM (#19478780)
Last I knew __FUNCTION__ in GCC doesn't even do name de-mangling. You'd get the raw C symbol name with no C++ types.
Re:Show HN: Nameof Operator for Modern C++ by neargye on Monday March 25, 2019 @04:05AM (#19480936)
Re:Show HN: Nameof Operator for Modern C++ by asveikau on Monday March 25, 2019 @10:15AM (#19482527)
Gcc 7. I could be wrong but I seem to recall a long time ago, when I was learning this stuff in the 90s, __FUNCTION__ wouldn't even demangle.
Re:Show HN: Nameof Operator for Modern C++ by neargye on Monday March 25, 2019 @01:16PM (#19484404)
yeap, I use __PRETTY_FUNCTION__.
Re:Show HN: Nameof Operator for Modern C++ by neargye on Monday March 25, 2019 @04:10AM (#19480951)
You are absolutely right :)
Re:Show HN: Nameof Operator for Modern C++ by asveikau on Sunday March 24, 2019 @05:03PM (#19478765)
That was what I was thinking, then I saw that this will stringify enum values, eg. nameof(c) returns "RED", which the preprocessor #x will not do.
Re:Show HN: Nameof Operator for Modern C++ by neargye on Monday March 25, 2019 @04:12AM (#19480959)
To be sure: nameof(c) returns "c" (nameof return name of variable) nameof_enum(c) returns "RED" (nameof_enum return enum name of enum variable)
Re:Show HN: Nameof Operator for Modern C++ by CalChris on Sunday March 24, 2019 @05:10PM (#19478806)
I only use STRINGIZE for error messages. I learned about it from reading the source for assert macros.
Re:Show HN: Nameof Operator for Modern C++ by gumby on Sunday March 24, 2019 @03:23PM (#19478222)
Nice work, and the header is nowhere near as complex as I had feared it would need to be.
Re:Show HN: Nameof Operator for Modern C++ by neargye on Monday March 25, 2019 @04:09AM (#19480946)
With features C++17 this header looks not so terribly.
Re:Show HN: Nameof Operator for Modern C++ by shhehebehdh on Sunday March 24, 2019 @06:14PM (#19479086)
I wonder what including and using this does to compile times. Nothing good, I suspect.
Re:Show HN: Nameof Operator for Modern C++ by neargye on Monday March 25, 2019 @03:59AM (#19480919)
In fact, not so much about the whole big project. May add a few seconds to the compilation.
Re:Show HN: Nameof Operator for Modern C++ by jhasse on Sunday March 24, 2019 @06:43PM (#19479198)
nameof_enum doesn't work with GCC :(
Re:Show HN: Nameof Operator for Modern C++ by neargye on Monday March 25, 2019 @03:54AM (#19480902)
Unfortunately, yes, I'm still looking for a solution for gcc
Re:Show HN: Nameof Operator for Modern C++ by neargye on Tuesday March 26, 2019 @08:16AM (#19491009)
New version supported GCC >= 9
Re:Show HN: Nameof Operator for Modern C++ by jhasse on Tuesday March 26, 2019 @08:20AM (#19491035)
Awesome :)