trait从成员函数类型中删除const? [英] trait to drop const from a member function type?
问题描述
当 T
是 double(float)const
当我尝试使用 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 qualifier (包括由 typedef-name (7.1.3,14.1)命名的
a类型)只能显示为:
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-id ,
- 类型参数(14.1)或模板参数的 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).
这篇关于trait从成员函数类型中删除const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!