nameof_module/doc/limitations.md

110 lines
3.5 KiB
Markdown
Raw Normal View History

2020-01-25 18:45:33 +00:00
# Limitations
## Nameof
2019-10-03 13:25:50 +00:00
* If argument does not have name, occurs the compilation error `"Expression does not have a name."`.
2020-01-25 18:45:33 +00:00
## Nameof Type
2019-10-03 13:25:50 +00:00
2019-10-03 13:56:36 +00:00
* To check is nameof type supported compiler use macro `NAMEOF_TYPE_SUPPORTED` or constexpr constant `nameof::is_nameof_type_supported`.
2019-10-03 13:30:48 +00:00
2019-10-03 13:25:50 +00:00
* This library uses a compiler-specific hack (based on `__PRETTY_FUNCTION__` / `__FUNCSIG__`), which works on Clang >= 5, MSVC >= 15.3 and GCC >= 7.
* Nameof type returns compiler-specific type name.
2019-10-03 13:30:48 +00:00
* If argument does not have name, occurs the compilation error `"Expression does not have a name."`.
2019-10-03 13:25:50 +00:00
2020-01-25 18:45:33 +00:00
## Nameof Enum
2019-10-03 13:25:50 +00:00
2019-10-03 13:56:36 +00:00
* To check is nameof enum supported compiler use macro `NAMEOF_ENUM_SUPPORTED` or constexpr constant `nameof::is_nameof_enum_supported`.
2019-10-03 13:30:48 +00:00
2019-10-03 13:25:50 +00:00
* This library uses a compiler-specific hack (based on `__PRETTY_FUNCTION__` / `__FUNCSIG__`), which works on Clang >= 5, MSVC >= 15.3 and GCC >= 9.
* Enum can't reflect if the enum is a forward declaration.
2020-01-25 18:45:33 +00:00
* Enum value must be in range `[NAMEOF_ENUM_RANGE_MIN, NAMEOF_ENUM_RANGE_MAX]`.
2019-10-03 13:25:50 +00:00
2020-01-25 18:45:33 +00:00
* By default `NAMEOF_ENUM_RANGE_MIN = -128`, `NAMEOF_ENUM_RANGE_MAX = 128`.
2019-10-03 13:25:50 +00:00
2020-01-25 18:45:33 +00:00
* `NAMEOF_ENUM_RANGE_MIN` must be less or equals than `0` and must be greater than `INT16_MIN`.
2019-10-03 13:25:50 +00:00
2020-01-25 18:45:33 +00:00
* `NAMEOF_ENUM_RANGE_MAX` must be greater than `0` and must be less than `INT16_MAX`.
2019-10-03 13:25:50 +00:00
2020-01-25 18:45:33 +00:00
* If need another range for all enum types by default, redefine the macro `NAMEOF_ENUM_RANGE_MIN` and `NAMEOF_ENUM_RANGE_MAX`.
```cpp
#define NAMEOF_ENUM_RANGE_MIN 0
#define NAMEOF_ENUM_RANGE_MAX 256
#include <nameof.hpp>
```
2020-02-14 13:04:48 +00:00
* If need another range for specific enum type, add specialization `enum_range` for necessary enum type. Specialization of `enum_range` must be injected in `namespace nameof`.
2020-01-25 18:45:33 +00:00
```cpp
#include <nameof.hpp>
2020-02-14 13:04:48 +00:00
enum class number { one = 100, two = 200, three = 300 };
2020-01-25 18:45:33 +00:00
namespace nameof {
template <>
struct enum_range<number> {
static constexpr int min = 100;
static constexpr int max = 300;
};
2020-02-14 13:04:48 +00:00
} // namespace nameof
2020-01-25 18:45:33 +00:00
```
* If you hit a message like this:
```text
[...]
note: constexpr evaluation hit maximum step limit; possible infinite loop?
2019-10-03 13:25:50 +00:00
```
2020-01-25 18:45:33 +00:00
Change the limit for the number of constexpr evaluated:
* MSVC `/constexpr:depthN`, `/constexpr:stepsN` <https://docs.microsoft.com/en-us/cpp/build/reference/constexpr-control-constexpr-evaluation>
* Clang `-fconstexpr-depth=N`, `-fconstexpr-steps=N` <https://clang.llvm.org/docs/UsersManual.html#controlling-implementation-limits>
* GCC `-fconstexpr-depth=N`, `-fconstexpr-loop-limit=N`, `-fconstexpr-ops-limit=N` <https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/C_002b_002b-Dialect-Options.html>
2019-10-03 13:56:36 +00:00
* Nameof enum obtains the first defined value enums, and won't work if value are aliased.
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
enum ShapeKind {
ConvexBegin = 0,
Box = 0, // Won't work.
Sphere = 1,
ConvexEnd = 2,
Donut = 2, // Won't work too.
Banana = 3,
COUNT = 4,
};
2019-10-03 13:56:36 +00:00
// nameof::nameof_enum(ShapeKind::Box) -> "ConvexBegin"
// NAMEOF_ENUM(ShapeKind::Box) -> "ConvexBegin"
2019-10-03 13:25:50 +00:00
```
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
Work around the issue:
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
enum ShapeKind {
// Convex shapes, see ConvexBegin and ConvexEnd below.
Box = 0,
Sphere = 1,
// Non-convex shapes.
Donut = 2,
Banana = 3,
COUNT = Banana + 1,
// Non-reflected aliases.
ConvexBegin = Box,
ConvexEnd = Sphere + 1,
};
2019-10-03 13:56:36 +00:00
// nameof::nameof_enum(ShapeKind::Box) -> "Box"
// NAMEOF_ENUM(ShapeKind::Box) -> "Box"
2019-10-03 13:25:50 +00:00
// Non-reflected aliases.
2019-10-03 13:56:36 +00:00
// nameof::nameof_enum(ShapeKind::ConvexBegin) -> "Box"
// NAMEOF_ENUM(ShapeKind::ConvexBegin) -> "Box"
2019-10-03 13:25:50 +00:00
```