::std::remove_cv<>应该处理函数类型吗?
should ::std::remove_cv<> work on function types?
我试图提取成员函数的返回和参数类型,而不必为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:
函数声明符中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.
}
相关文章:
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 是否有任何建议来统一函数类型限定符并简化可恶的函数类型?
- 关于 C++ 中的函数类型定义
- 用于检测函数类型是否为否的特征
- 函数类型参数的模板参数推导
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++无效的函数类型转换
- STL 函数和函数类型与函数指针类型
- 如何将result_of与函数类型定义一起使用
- 将模板(没有规范)传递给 std::thread() 会出现错误:<未解析的重载函数类型>匹配错误
- C++ 编译错误:gnu_printf是无法识别的格式函数类型
- 专门用于"direct"函数类型(与函数指针类型相对)
- 为每次处理特定类型的模板化函数提供多个实现
- (c++)如何制作一个可以处理不同类型和数量参数的函数
- 如何编写一个通用函数来处理不同类型的流
- 处理特定类型元素的任何容器的非模板函数
- 在处理函数的部分模板专门化时,类型定义和参数包展开问题
- ::std::remove_cv<>应该处理函数类型吗?
- C++如何泛化类函数参数来处理多种类型的函数指针