Search code examples
c++functionc++11type-traits

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...>; };

Solution

  • 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.