trait to drop const from a member function type?(从成员函数类型中删除 const 的特征?)
问题描述
当 T
为 double(float)const
时,当我尝试使用 function<T>
时出现此错误.
When T
is double(float)const
I get this error when I try to use function<T>
.
implicit instantiation of undefined template 'std::function<double (float) const>'
但是当T
为double(float)
时就可以了.我尝试使用 std::remove_cv<T>::type
来删除这个 const
,但这不起作用.是的,我有 #include<functional>
.
But it's OK when T
is double(float)
. I tried to use std:: remove_cv<T>::type
to remove this const
, but that doesn't work. And yes, I have #include<functional>
.
所以我的主要问题是:如何解决这个问题并删除 const
以便我可以将此函数类型放入 std::function
.?
So my main question is: How to fix this and remove const
so that I can put this function type into std:: function
.?
我在使用 lambdas 的 operator()
方法时遇到了这个问题,但我认为这个问题通常是关于任何方法类型的,而不仅仅是 lambdas
I came across this issue when working with the operator()
method of lambdas, but I think this question is generally about any method type, not just for lambdas
但我的第二个问题是:double(float)const
甚至是什么意思?!!我能理解
But my second question is: What does double(float)const
even mean ?!! I can understand
double (ClassName::) (float) const
因为这意味着成员函数不能修改它的 ClassName
对象.当我将此类型放入模板以删除类类型时,我得到 double(float)const
导致麻烦.
as it means the member function cannot modify its ClassName
object. When I put this type into a template to remove the class type, then I get the double(float)const
which is causing trouble.
template<typename>
struct DropClassType;
template<typename Sig, typename C>
struct DropClassType<Sig (C::*)> {
typedef Sig type_without_class;
};
(clang 3.4.2.g++-4.9.1的错误比较隐晦,但基本相同)
(clang 3.4.2. The errors from g++-4.9.1 are more cryptic, but basically the same)
推荐答案
为什么会出现未定义模板的隐式实例化"错误?
Why did I get the "implicit instantiation of undefined template" error?
std::function
被定义为未定义的基本模板和匹配正常"函数类型的部分特化(§20.9.11.2 [func.wrap.func]):
std::function
is defined as an undefined base template and a partial specialization that matches "normal" function types (§20.9.11.2 [func.wrap.func]):
template<class> class function; // undefined
template<class R, class... ArgTypes>
class function<R(ArgTypes...)> { /* ... */ };
double (float) const
与 R(ArgTypes...)
不匹配,因此您将获得未定义的基本模板.
double (float) const
doesn't match R(ArgTypes...)
, so you get the undefined base template instead.
如何解决这个问题并删除 const 以便我可以将此函数类型放入 std::function
?
How to fix this and remove const so that I can put this function type into
std::function
?
标准的偏特化技巧.在我们处理它的同时,让我们也删除 volatile
.
The standard partial specialization trick. While we are at it, let's also remove volatile
.
template<class> class rm_func_cv; // undefined
template<class R, class... ArgTypes>
class rm_func_cv<R(ArgTypes...)> { using type = R(ArgTypes...); };
template<class R, class... ArgTypes>
class rm_func_cv<R(ArgTypes...) const> { using type = R(ArgTypes...); };
template<class R, class... ArgTypes>
class rm_func_cv<R(ArgTypes...) volatile> { using type = R(ArgTypes...); };
template<class R, class... ArgTypes>
class rm_func_cv<R(ArgTypes...) const volatile> { using type = R(ArgTypes...); };
当然,可以使用类似的技巧来删除 ref-qualifiers.
Similar tricks can be used to remove ref-qualifiers, of course.
double (float) const
是什么意思?!!
这是标准中一个相当晦涩的角落(§8.3.5 [dcl.fct]/p6):
This is a rather obscure corner of the standard (§8.3.5 [dcl.fct]/p6):
具有 cv-qualifier-seq 或 ref-qualifier 的函数类型(包括由 typedef-name (7.1.3, 14.1)) 命名的类型应仅显示为:
A function type with a cv-qualifier-seq or a ref-qualifier (including a type named by typedef-name (7.1.3, 14.1)) shall appear only as:
- 非静态成员函数的函数类型,
- 指向成员的指针所指的函数类型,
- 函数类型定义声明或别名声明的顶级函数类型,
- type-parameter (14.1) 的默认参数中的 type-id,或
- 模板参数的type-id用于type-parameter (14.3.1).
- the function type for a non-static member function,
- the function type to which a pointer to member refers,
- the top-level function type of a function typedef declaration or alias-declaration,
- the type-id in the default argument of a type-parameter (14.1), or
- the type-id of a template-argument for a type-parameter (14.3.1).
[例子:
typedef int FIC(int) const;
FIC f; // ill-formed: does not declare a member function
struct S {
FIC f; // OK
};
FIC S::*pm = &S::f; // OK
—结束示例 ]
简而言之,它基本上是半个类型",您可以使用它来声明类成员函数或指向成员的类型(或作为模板参数传递).
In short, it's basically "half a type" that you can use to declare a class member function or a pointer-to-member type (or pass as a template parameter).
这篇关于从成员函数类型中删除 const 的特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从成员函数类型中删除 const 的特征?
基础教程推荐
- 如何C++使用模板特化功能 2023-03-05
- 一文带你了解C++中的字符替换方法 2023-07-20
- C++使用easyX库实现三星环绕效果流程详解 2023-06-26
- C利用语言实现数据结构之队列 2022-11-22
- C语言基础全局变量与局部变量教程详解 2022-12-31
- C++中的atoi 函数简介 2023-01-05
- 详解c# Emit技术 2023-03-25
- C++详细实现完整图书管理功能 2023-04-04
- C/C++编程中const的使用详解 2023-03-26
- C语言 structural body结构体详解用法 2022-12-06