should :: std :: remove_cv<>在功能类型上工作?

时间:2016-08-04 12:56:47

标签: c++ c++17

我正在尝试提取成员函数的返回和参数类型,而不必费心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());
}

1 个答案:

答案 0 :(得分:2)

没有const函数类型:

[dcl.fct] / 6:

  

cv-qualifier-seq在函数声明符中的作用与在顶部添加cv-qualification不同   功能类型。在后一种情况下,忽略cv限定符。 [注意:有一个函数类型   cv-qualifier-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.
}
相关问题