从成员函数类型中删除 const 的特征?

trait to drop const from a member function type?(从成员函数类型中删除 const 的特征?)
本文介绍了从成员函数类型中删除 const 的特征?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tdouble(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>'

但是当Tdouble(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) constR(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-seqref-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 的特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)