是否有可能使用特征或其他方法来确定函数(或方法)的值参数是否被声明为const?例如,在下面的代码中,可以使用fn_params
来查找第一个参数的类型为const int *
;然而第二个参数仅被识别为具有类型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...>; };
参数的顶级'const'在函数的类型中被忽略。 – chris
这是什么用例?如果(!is_const(x))x = 42'尝试'不会编译,即使'is_const'可以工作。 –
如果单个const参数不会被忽略,那么该特征看起来像这样http://coliru.stacked-crooked.com/a/b1316c1ba5e284c4。 –