std::meta::parameters_of
| Defined in header <meta>
|
||
consteval std::vector<std::meta::info> parameters_of( std::meta::info r );
|
(since C++26) | |
If r represents a function, returns a vector containing reflections of its function parameters.
If r represents a function type, returns a vector containing reflections of its parameter types.
In both cases, the elements of the vector are in the order in which the parameters appear.
Parameters
| r | - | a reflection that represents a function or function type |
Return value
A std::vector of reflections.
Exceptions
Throws std::meta::exception if r does not represent a function or function type.
Notes
A reflection of function parameter can be queried for the parameter's type, identifier (if any), and list of annotations. To get the value of the parameter variable within the function body, see std::meta::variable_of.
The type of a parameter (as returned by std::meta::type_of) is cv-unqualified, which may differ from the type of the corresponding variable:
void f(const int x)
{
constexpr auto rp = std::meta::parameters_of(^^f)[0];
static_assert(std::meta::type_of(rp) == ^^int);
constexpr auto rv = std::meta::variable_of(rp);
static_assert(&[:rv:] == &x);
static_assert(std::meta::type_of(rv) == ^^const int);
}
Example
#include <meta>
char func(float a, long b = 0);
static_assert(std::meta::type_of(^^func) == ^^char(float, long));
constexpr auto func_param_0{ parameters_of(^^func)[0] },
func_param_1{ parameters_of(^^func)[1] };
static_assert(
(std::meta::return_type_of(^^func) == ^^char) and
(std::meta::type_of(func_param_0) == ^^float) and
(std::meta::type_of(func_param_1) == ^^long) and
(std::meta::identifier_of(func_param_0) == "a") and
(std::meta::identifier_of(func_param_1) == "b") and
(std::meta::has_default_argument(func_param_0) == false) and
(std::meta::has_default_argument(func_param_1) == true)
);
constexpr auto func_type_r{ std::meta::type_of(^^func) };
static_assert(
(std::meta::return_type_of(func_type_r) == ^^char) and
(parameters_of(func_type_r)[0] == ^^float) and
(parameters_of(func_type_r)[1] == ^^long)
);
int main() {}
See also
(C++26) |
obtains the variable of the reflected function parameter in the function definition (function) |
(C++26) |
checks if reflection represents a function parameter (function) |
(C++26) |
obtains the return type of the reflected function (function) |