简体   繁体   中英

Trait for const-ness of a function's value parameters?

Is it possible to use traits, or other methods, to determine whether the value parameters of a function (or method) were declared const? For example, in the code below, fn_params can be used to find that the 1st parameter has const int * type; however the 2nd parameter is identified only as having type double .

float foo(const int *, const double) { return 7.6f; }

template <typename T, typename ...Ts>
struct fn_params<T(*)(Ts...)> { using type = std::tuple<Ts...>; };

No, there is not, principally because there is no way to determine what parameters a function can take. Your query is ambiguous even in simple cases like templates, which could be explicitly instantiated with const types even if the normal rules of deduction would make the parameters non-const, let alone overloads and similar matters.

Before you can ask if the parameters are const, you must know what the parameters are, and that is impossible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM