2018-03-17 04:31:59 +00:00
|
|
|
# nameof(x) c++
|
|
|
|
|
2018-03-17 05:15:00 +00:00
|
|
|
C++ alternative to [nameOf](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof) operator.
|
|
|
|
|
2018-03-17 04:31:59 +00:00
|
|
|
Simple Example:
|
2018-03-17 08:18:42 +00:00
|
|
|
```cpp
|
2018-03-17 04:31:59 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <nameof.hpp>
|
2018-03-17 05:15:00 +00:00
|
|
|
|
2018-03-17 04:31:59 +00:00
|
|
|
struct SomeStruct {
|
|
|
|
int SomeField;
|
|
|
|
void SomeMethod() { std::cout << "No called!" << std::endl; }
|
|
|
|
};
|
|
|
|
|
|
|
|
int someVar = 0;
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SomeStruct someVar{1};
|
|
|
|
|
2018-03-17 05:15:00 +00:00
|
|
|
constexpr auto a = NAMEOF_VAR(someVar.SomeField);
|
|
|
|
constexpr auto b = NAMEOF_VAR((&someVar)->SomeField);
|
|
|
|
constexpr auto c = NAMEOF_VAR(someVar);
|
|
|
|
constexpr auto d = NAMEOF_VAR(::someVar);
|
|
|
|
constexpr auto e = NAMEOF_VAR(&SomeStruct::SomeMethod);
|
|
|
|
constexpr auto f = NAMEOF_FUN(someVar.SomeMethod());
|
|
|
|
constexpr auto g = NAMEOF_TYPE(SomeStruct);
|
2018-03-17 04:31:59 +00:00
|
|
|
|
|
|
|
std::cout << a << std::endl; // SomeField
|
|
|
|
std::cout << b << std::endl; // SomeField
|
|
|
|
std::cout << c << std::endl; // someVar
|
|
|
|
std::cout << d << std::endl; // someVar
|
|
|
|
std::cout << e << std::endl; // SomeMethod
|
|
|
|
std::cout << f << std::endl; // SomeMethod()
|
|
|
|
std::cout << g << std::endl; // SomeStruct
|
|
|
|
}
|
|
|
|
```
|