nameof_module/doc/reference.md

462 lines
13 KiB
Markdown
Raw Normal View History

2019-10-03 13:25:50 +00:00
# Reference
2020-12-29 18:45:54 +00:00
* [`NAMEOF` obtains simple name of variable, function, macro.](#nameof)
* [`NAMEOF_FULL` obtains full name of variable, function, macro.](#nameof_full)
* [`NAMEOF_RAW` obtains raw name of variable, function, macro.](#nameof_raw)
* [`NAMEOF_ENUM` obtains name of enum variable.](#nameof_enum)
2022-07-26 14:48:58 +00:00
* [`NAMEOF_ENUM_OR` Obtains name of enum variable or default value if enum variable out of range.](#nameof_enum_or)
2020-12-29 18:45:54 +00:00
* [`NAMEOF_ENUM_CONST` obtains name of static storage enum variable.](#nameof_enum_const)
* [`NAMEOF_ENUM_FLAG` obtains name of enum-flags variable.](#nameof_enum_flag)
* [`NAMEOF_TYPE` obtains type name.](#nameof_type)
* [`NAMEOF_FULL_TYPE` obtains full type name.](#nameof_full_type)
* [`NAMEOF_SHORT_TYPE` obtains short type name.](#nameof_short_type)
* [`NAMEOF_TYPE_EXPR` obtains type name of expression.](#nameof_type_expr)
* [`NAMEOF_FULL_TYPE_EXPR` obtains full type name of expression.](#nameof_full_type_expr)
* [`NAMEOF_SHORT_TYPE_EXPR` obtains short type name of expression.](#nameof_short_type_expr)
* [`NAMEOF_TYPE_RTTI` obtains type name, using RTTI.](#nameof_type_rtti)
* [`NAMEOF_FULL_TYPE_RTTI` obtains short type name, using RTTI.](#nameof_full_type_rtti)
* [`NAMEOF_SHORT_TYPE_RTTI` obtains short type name, using RTTI.](#nameof_short_type_rtti)
2023-03-30 15:10:22 +00:00
* [`NAMEOF_MEMBER` obtains name of member.](#nameof_member)
* [`NAMEOF_POINTER` obtains name of a function, a global or class static variable.](#nameof_pointer)
2019-10-03 13:25:50 +00:00
2020-01-25 18:45:33 +00:00
## Synopsis
2019-10-03 13:30:48 +00:00
* Before use, read the [limitations](limitations.md) of functionality.
2023-03-30 15:10:22 +00:00
* To check is nameof_enum supported compiler use macro `NAMEOF_ENUM_SUPPORTED` or constexpr constant `nameof::is_nameof_enum_supported`.</br>
If nameof_enum used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_ENUM_NO_CHECK_SUPPORT`.
2020-01-25 18:45:33 +00:00
* To check is nameof_type supported compiler use macro `NAMEOF_TYPE_SUPPORTED` or constexpr constant `nameof::is_nameof_type_supported`.</br>
If nameof_type used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`.
2019-10-03 13:30:48 +00:00
2020-12-29 18:45:54 +00:00
* To check is nameof_type_rtti supported compiler use macro `NAMEOF_TYPE_RTTI_SUPPORTED` or constexpr constant `nameof::is_nameof_type_rtti_supported`.</br>
If nameof_type used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`.
2023-03-30 15:10:22 +00:00
* To check is nameof_member supported compiler use macro `NAMEOF_MEMBER_SUPPORTED` or constexpr constant `nameof::is_nameof_member_supported`.</br>
If nameof_member used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`.
* To check is nameof_pointer supported compiler use macro `NAMEOF_POINTER_SUPPORTED` or constexpr constant `nameof::is_nameof_pointer_supported`.</br>
If nameof_pointer used on unsupported compiler, occurs the compilation error. To suppress error define macro `NAMEOF_TYPE_NO_CHECK_SUPPORT`.
2019-10-03 13:30:48 +00:00
2021-01-12 08:39:52 +00:00
* To add custom enum or type names see the [example](../example/example_custom_name.cpp).
* To change the type of strings, use special macros:
```cpp
#include <my_lib/string.hpp>
#include <my_lib/string_view.hpp>
#define NAMEOF_USING_ALIAS_STRING using string = my_lib::String;
#define NAMEOF_USING_ALIAS_STRING_VIEW using string_view = my_lib::StringView;
#include <nameof.hpp>
```
2019-10-03 13:25:50 +00:00
## `NAMEOF`
2022-07-26 14:48:58 +00:00
* Obtains name of variable, function, macro.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `nameof::cstring` - constexpr implementation of an string. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
* If argument does not have name, occurs the compilation error `"Expression does not have a name."`.
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
// Name of variable.
NAMEOF(somevar) -> "somevar"
// Name of member variable.
NAMEOF(person.address.zip_code) -> "zip_code"
// Name of function.
NAMEOF(foo<int, float>()) -> "foo"
// Name of member function.
NAMEOF(somevar.some_method()) -> "some_method"
NAMEOF(somevar.some_method<int>()) -> "some_method"
// Name of macro.
NAMEOF(__LINE__) -> "__LINE__"
NAMEOF(NAMEOF(structvar)) -> "NAMEOF"
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 7 and C++ >= 17</br>
2019-10-03 13:25:50 +00:00
## `NAMEOF_FULL`
2022-07-26 14:48:58 +00:00
* Obtains full (with template suffix) name of variable, function, macro.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `nameof::cstring` - constexpr implementation of an string. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
* If argument does not have name, occurs the compilation error `"Expression does not have a name."`.
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
// Full name of template function.
NAMEOF_FULL(foo<int, float>()) -> "foo<int, float>"
// Full name of template member function.
NAMEOF_FULL(somevar.some_method<int>()) -> "some_method<int>"
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 7 and C++ >= 17</br>
2019-10-03 13:25:50 +00:00
## `NAMEOF_RAW`
2020-12-29 18:45:54 +00:00
* Obtains raw name of variable, function, macro.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `nameof::cstring` - constexpr implementation of an string. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
* If argument does not have name, occurs the compilation error `"Expression does not have a name."`.
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
NAMEOF_RAW(::somevar.somefield) -> "::somevar.somefield"
NAMEOF_RAW(&some_class::some_method<int>) -> "&some_class::some_method<int>"
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 7 and C++ >= 17</br>
2019-10-03 13:25:50 +00:00
## `NAMEOF_ENUM`
2022-07-26 14:48:58 +00:00
* Obtains name of enum variable.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `string_view`. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* If argument does not have name or [out of range](limitations.md#nameof-enum), returns empty `string_view`, in debug occurs assert.
2019-10-03 13:25:50 +00:00
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
auto color = Color::RED;
NAMEOF_ENUM(color) -> "RED"
2020-12-29 18:45:54 +00:00
nameof::nameof_enum(color) -> "RED"
2019-10-03 13:25:50 +00:00
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 9 and C++ >= 17</br>
2022-07-26 14:48:58 +00:00
# `NAMEOF_ENUM_OR`
* Obtains name of enum variable or default value if enum variable out of range.
* Returns `string`.
* If argument does not have name or [out of range](limitations.md#nameof-enum), returns `default_value`.
2023-03-30 15:10:22 +00:00
```cpp
auto color = Color::RED;
NAMEOF_ENUM_OR(color, "none") -> "RED"
NAMEOF_ENUM_OR((Color)-1, "none") -> "none"
nameof::nameof_enum_or(color, "none") -> "RED"
nameof::nameof_enum_or((Color)-1, "none") -> "none"
```
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 9 and C++ >= 17</br>
2020-09-07 17:12:54 +00:00
## `NAMEOF_ENUM_CONST`
2019-10-03 13:25:50 +00:00
2022-07-26 14:48:58 +00:00
* Obtains name of static storage enum variable.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `string_view`. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
2020-01-25 18:45:33 +00:00
* This version is much lighter on the compile times and is not restricted to the enum_range [limitation](limitations.md#nameof-enum).
2019-10-03 13:25:50 +00:00
2020-01-25 18:45:33 +00:00
* If argument does not have name, occurs the compilation error `"Enum value does not have a name."`.
2019-10-03 13:25:50 +00:00
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
2020-09-07 17:12:54 +00:00
NAMEOF_ENUM_CONST(Color::GREEN) -> "GREEN"
2020-12-29 18:45:54 +00:00
nameof::nameof_enum<Color::GREEN>() -> "GREEN"
2020-07-03 13:18:05 +00:00
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 9 and C++ >= 17</br>
2020-07-03 13:18:05 +00:00
## `NAMEOF_ENUM_FLAG`
2022-07-26 14:48:58 +00:00
* Obtains name of enum flag variable.
2020-07-03 13:18:05 +00:00
2020-12-29 18:45:54 +00:00
* Returns `string`.
2020-07-03 13:18:05 +00:00
2020-12-29 18:45:54 +00:00
* If argument does not have name or [out of range](limitations.md#nameof-enum), returns empty `string`, in debug occurs assert.
2020-07-03 13:18:05 +00:00
* Examples
```cpp
enum class AnimalFlags { HasClaws = 1 << 0, CanFly = 1 << 1, EatsFish = 1 << 2, Endangered = 1 << 3 };
auto flag = AnimalFlags::Endangered;
2020-12-29 18:45:54 +00:00
2020-07-03 13:18:05 +00:00
NAMEOF_ENUM_FLAG(flag) -> "Endangered"
2020-12-29 18:45:54 +00:00
nameof_enum_flag(flag) -> "Endangered"
flag = AnimalFlags::CanFly | AnimalFlags::Endangered;
2020-07-03 13:18:05 +00:00
NAMEOF_ENUM_FLAG(flag) -> "CanFly|Endangered"
2020-12-29 18:45:54 +00:00
nameof_enum_flag(flag) -> "CanFly|Endangered"
2020-07-03 13:18:05 +00:00
NAMEOF_ENUM(HasClaws | CanFly) -> ""
2020-12-29 18:45:54 +00:00
nameof_enum(HasClaws | CanFly) -> ""
2020-07-03 13:18:05 +00:00
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 9 and C++ >= 17</br>
2020-12-29 18:45:54 +00:00
## `NAMEOF_TYPE`
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Obtains type name, reference and cv-qualifiers are ignored.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `string_view`. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* In all cases, reference and cv-qualifiers are ignored by `NAMEOF_TYPE` (that is, `NAMEOF_TYPE(const T&) == NAMEOF_TYPE(T)`).
2019-10-03 13:25:50 +00:00
* Returns compiler-specific type name.
2020-12-29 18:45:54 +00:00
* If type does not have name, occurs the compilation error `"Type does not have a name."`.
2019-10-03 13:25:50 +00:00
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
using T = const int&;
2020-12-29 18:45:54 +00:00
NAMEOF_TYPE(T) -> "int"
2019-10-03 13:25:50 +00:00
nameof::nameof_type<T>() -> "int"
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 7 and C++ >= 17</br>
2020-12-29 18:45:54 +00:00
## `NAMEOF_FULL_TYPE`
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Obtains full type name, with reference and cv-qualifiers.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `string_view`. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
* Returns compiler-specific type name.
2020-12-29 18:45:54 +00:00
* If type does not have name, occurs the compilation error `"Type does not have a name."`.
2019-10-03 13:25:50 +00:00
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
using T = const int&;
2020-12-29 18:45:54 +00:00
NAMEOF_TYPE(T) -> "const int&"
2019-10-03 13:25:50 +00:00
nameof::nameof_full_type<T>() -> "const int&"
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 7 and C++ >= 17</br>
2020-12-29 18:45:54 +00:00
## `NAMEOF_SHORT_TYPE`
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Obtains short type name.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `string_view`. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
* Returns compiler-specific type name.
2020-12-29 18:45:54 +00:00
* If type does not have name, occurs the compilation error `"Type does not have a name."`.
2019-10-03 13:25:50 +00:00
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
2020-12-29 18:45:54 +00:00
using T = const my::detail::SomeClass<int>&;
NAMEOF_SHORT_TYPE(T) -> "SomeClass"
nameof::nameof_short_type<T>() -> "SomeClass"
2019-10-03 13:25:50 +00:00
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 7 and C++ >= 17</br>
2020-12-29 18:45:54 +00:00
## `NAMEOF_TYPE_EXPR`
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Obtains string name type of expression, reference and cv-qualifiers are ignored.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `string_view`. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
* Returns compiler-specific type name.
2020-12-29 18:45:54 +00:00
* In all cases, reference and cv-qualifiers are ignored.
* If type does not have name, occurs the compilation error `"Type does not have a name."`.
2019-10-03 13:25:50 +00:00
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
using T = const int&;
2020-12-29 18:45:54 +00:00
T var = 42;
NAMEOF_TYPE_EXPR(var) -> "int"
nameof::nameof_type<decltype(var)>() -> "int"
2019-10-03 13:25:50 +00:00
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 7 and C++ >= 17</br>
2020-12-29 18:45:54 +00:00
## `NAMEOF_FULL_TYPE_EXPR`
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Obtains full type name of expression, with reference and cv-qualifiers.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `string_view`. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns compiler-specific type name.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* If type does not have name, occurs the compilation error `"Type does not have a name."`.
2019-10-03 13:25:50 +00:00
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
using T = const int&;
T var = 42;
2020-12-29 18:45:54 +00:00
NAMEOF_FULL_TYPE_EXPR(var) -> "const int&"
nameof::nameof_full_type<decltype(var)>() -> "const int&"
2019-10-03 13:25:50 +00:00
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 7 and C++ >= 17</br>
2020-12-29 18:45:54 +00:00
## `NAMEOF_SHORT_TYPE_EXPR`
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Obtains short type name of expression.
2019-10-03 13:25:50 +00:00
2020-12-29 18:45:54 +00:00
* Returns `string_view`. Marked `constexpr` and `noexcept`.
2019-10-03 13:25:50 +00:00
* Returns compiler-specific type name.
2020-12-29 18:45:54 +00:00
* If type does not have name, occurs the compilation error `"Type does not have a name."`.
2019-10-03 13:25:50 +00:00
* Examples
2020-01-25 18:45:33 +00:00
2019-10-03 13:25:50 +00:00
```cpp
2020-12-29 18:45:54 +00:00
const my::detail::SomeClass<int> var;
NAMEOF_SHORT_TYPE_EXPR(var) -> "SomeClass"
nameof::nameof_short_type<decltype(var)>() -> "SomeClass"
2019-10-03 13:25:50 +00:00
```
2020-06-06 11:14:16 +00:00
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2017 and C++ >= 17</br>
GCC >= 7 and C++ >= 17</br>
2020-06-06 11:14:16 +00:00
## `NAMEOF_TYPE_RTTI`
2020-12-29 18:45:54 +00:00
* Obtains type name, using RTTI.
2020-06-06 11:14:16 +00:00
2020-12-29 18:45:54 +00:00
* Returns `string`.
2020-06-06 11:14:16 +00:00
* Examples
```cpp
2020-12-29 18:45:54 +00:00
volatile const my::detail::Base* ptr = new my::detail::Derived();
NAMEOF_TYPE_RTTI(*ptr) -> "my::detail::Derived"
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17 and RTTI enabled</br>
Visual Studio >= 2017 and C++ >= 17 and RTTI enabled</br>
GCC >= 7 and C++ >= 17 and RTTI enabled</br>
2020-12-29 18:45:54 +00:00
## `NAMEOF_FULL_TYPE_RTTI`
* Obtains full type name, using RTTI.
* Returns `string`.
2020-06-06 11:14:16 +00:00
2020-12-29 18:45:54 +00:00
* Examples
```cpp
volatile const my::detail::Base* ptr = new my::detail::Derived();
NAMEOF_FULL_TYPE_RTTI(cv_ref) -> "volatile const my::detail::Derived&"
``
## `NAMEOF_SHORT_TYPE_RTTI`
* Obtains short type name, using RTTI.
* Returns `string`.
* Examples
```cpp
volatile const my::detail::Base* ptr = new my::detail::Derived();
NAMEOF_SHORT_TYPE_RTTI(*ptr) -> "Derived"
2020-06-06 11:14:16 +00:00
```
2023-03-30 15:10:22 +00:00
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17 and RTTI enabled</br>
Visual Studio >= 2017 and C++ >= 17 and RTTI enabled</br>
GCC >= 7 and C++ >= 17 and RTTI enabled</br>
## `NAMEOF_MEMBER`
* Obtains name of member.
* Returns `string_view`.
* Examples
```cpp
struct A {
int this_is_the_name;
};
// ..
NAMEOF_MEMBER(&A::this_is_the_name) -> "this_is_the_name"
nameof::nameof_member(&A::this_is_the_name) -> "this_is_the_name"
```
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2022 and C++ >= 20</br>
GCC >= 7 and C++ >= 17</br>
## `NAMEOF_POINTER`
* Obtains name of a function, a global or class static variable.
* Returns `string_view`.
* Examples
```cpp
int someglobalvariable = 0;
// ..
NAMEOF_POINTER(&someglobalconstvariable) == "someglobalconstvariable"
nameof::nameof_pointer(&someglobalconstvariable) == "someglobalconstvariable"
constexpr auto global_ptr = &someglobalvariable;
NAMEOF_POINTER(global_ptr) == "someglobalconstvariable"
nameof::nameof_pointer(global_ptr) == "someglobalconstvariable"
```
* Compiler compatibility
Clang/LLVM >= 5 and C++ >= 17</br>
Visual Studio >= 2022 and C++ >= 20</br>
GCC >= 7 and C++ >= 17</br>