user1095108 user1095108 - 4 months ago 6
C++ Question

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

I'm trying to extract the return and argument types of a member function without having to bother with

const
and
volatile
overloads, but it seems to me, that
::std::remove_cv<>
does not work with function types:

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());
}

Answer

There is no such thing as a const function type:

[dcl.fct]/6:

The effect of a cv-qualifier-seq in a function declarator is not the same as adding cv-qualification on top of the function type. In the latter case, the cv-qualifiers are ignored. [Note: a function type that has a cv-qualifier-seq is not a cv-qualified type; there are no cv-qualified function types. —end note]

You will have to write your own type trait:

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