::std::remove_cv<> 应该处理函数类型吗?
should ::std::remove_cv<> work on function types?
我正在尝试提取成员函数的 return 和参数类型,而不必为 const
和 volatile
重载而烦恼,但在我看来,::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.
}
我正在尝试提取成员函数的 return 和参数类型,而不必为 const
和 volatile
重载而烦恼,但在我看来,::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.
}