xtd 0.2.0
Loading...
Searching...
No Matches

◆ nameof_

#define nameof_ (   ...)

#include <xtd.core/include/xtd/nameof.h>

Used to obtain the simple (unqualified) string name of a variable, type, or member.

Namespace
xtd
Library
xtd.core

When reporting errors in code, hooking up model-view-controller (MVC) links, firing property changed events, etc., you often want to capture the string name of a method. Using nameof helps keep your code valid when renaming definitions. Before, you had to use string literals to refer to definitions, which is brittle when renaming code elements because tools do not know to check these string literals.

A nameof expression has this form:

if (x == null) throw std::argument_error(nameof_(x));
write_line(nameof_(person.address().zip_code())); // prints "person.address().zip_code()"
#define nameof_(...)
Used to obtain the simple (unqualified) string name of a variable, type, or member.
Definition nameof.h:34
Examples
Some C++ examples:
Remarks
The argument to nameof must be a simple name, qualified name, member access, base access with a specified member, or this access with a specified member. The argument expression identifies a code definition, but it is never evaluated.
Because the argument needs to be an expression syntactically, there are many things disallowed that are not useful to list. The following are worth mentioning that produce errors: predefined types (for example, int or void), nullable types (Point?), array types (Customer[,]), pointer types (Buffer*), qualified alias (A::B), and unbound generic types (Dictionary<,>), preprocessing symbols (DEBUG), and labels (loop:).
If you need to get the fully-qualified name, you can use the typeof_ expression along with nameof. For example:
class c {
public:
void f(int i) {
log(xtd::string::format("{0}.{1}", typeof_(C), nameof_(f)), "method entry");
}
}
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
#define typeof_
Used to obtain the type object of a specified type or object.
Definition typeof.h:45