Contrary to all other similar
questions, this question is about using the new C++
features.
After reading
many answers, I did not yet find
any:
Example
An
example is often better than a long explanation.
You can compile and run this
snippet on rel="noreferrer">Coliru.
( href="http://coliru.stacked-crooked.com/a/bb58819257b6df50" rel="noreferrer">Another
former example is also
available)
#include
Constraints
- Please
no valueless duplication of href="https://stackoverflow.com/a/10177840/938111">other answers or href="https://stackoverflow.com/a/18791363/938111">basic
link. - Please avoid bloat macro-based answer, or
try to reduce the#define
overhead as minimum as possible.
- Please no manual
enum
->
string
mapping.
Nice to
have
- Support
enum
values starting from a number different from
zero - Support negative
enum
values - Support fragmented
enum
values - Support
class enum
(C++11) - Support
class enum :
having any allowed
(C++11) - Compile-time (not run-time) conversions to a
string,
or at least fast execution at run-time (e.g.
std::map
is not a great
idea...) - href="https://en.cppreference.com/w/cpp/language/constexpr"
rel="noreferrer">constexpr
(C++11, then relaxed in
C++14/17/20) - href="https://en.cppreference.com/w/cpp/language/noexcept"
rel="noreferrer">noexcept
(C++11) - href="https://en.wikipedia.org/wiki/C%2B%2B17" rel="noreferrer">C++17/ href="https://en.wikipedia.org/wiki/C%2B%2B20" rel="noreferrer">C++20
friendly snippet
One
possible idea could be using the C++ compiler capabilities to generate C++ code at
compilation-time using meta-programming tricks based on variadic template
and
classconstexpr
functions...
No comments:
Post a Comment