::std::remove_cv<>应该处理函数类型吗?

should ::std::remove_cv<> work on function types?

本文关键字:处理函数 类型 gt cv remove lt std      更新时间:2023-10-16

我试图提取成员函数的返回和参数类型,而不必为constvolatile重载而烦恼,但在我看来,::std::remove_cv<>不适用于函数类型:

template <typename>
struct signature
{
};
template <typename R, typename ...A>
struct signature<R(A...)>
{
};
template <typename C, typename F>
constexpr auto extract_function_type(F C::* const) noexcept
{
  return signature<::std::remove_cv_t<F>>();
}
template <typename F>
constexpr auto extract_signature(F const&) noexcept ->
  decltype(&F::operator(), extract_function_type(&F::operator()))
{
  return extract_function_type(&F::operator());
}

没有const函数类型:

[dcl.fct]/6:

函数声明符中cv限定符seq的效果与在顶部添加cv限定不同函数类型的。在后一种情况下,cv限定符将被忽略。[注意:具有cv限定符seq不是cv限定类型;没有cv限定的函数类型--尾注]

你必须写下你自己的类型特征:

template<typename T>
struct remove_cv_seq;
template<typename R, typename... Args>
struct remove_cv_seq<R (Args...) const> {
    using type = R (Args...);
};
template<typename R, typename... Args>
struct remove_cv_seq<R (Args...)> {
    using type = R (Args...);
};
struct Foo {
    remove_cv_seq<void () const>::type bar;
};
int main()
{
    Foo const x;
    x.bar(); // This will fail to compile because it tries to call non-const member function.
}