gpt4 book ai didi

c++ - should::std::remove_cv<> 适用于函数类型吗?

转载 作者:搜寻专家 更新时间:2023-10-31 02:15:15 28 4
gpt4 key购买 nike

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

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]

您必须编写自己的类型特征:

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

关于c++ - should::std::remove_cv<> 适用于函数类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38767993/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com