加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 从成员函数类型中删除const的trait?

发布时间:2020-12-16 06:01:41 所属栏目:百科 来源:网络整理
导读:当T是double(float)const时,当我尝试使用函数 T时,我得到这个错误. implicit instantiation of undefined template 'std::functiondouble (float) const' 但是当T为双倍(float)时,可以.我试图使用std :: remove_cv T :: type来删除这个const,但是这不行.是的
当T是double(float)const时,当我尝试使用函数< T>时,我得到这个错误.
implicit instantiation of undefined template 'std::function<double (float) const>'

但是当T为双倍(float)时,可以.我试图使用std :: remove_cv< T> :: type来删除这个const,但是这不行.是的,我有#include< functional&gt ;. 所以我的主要问题是:如何修复这个并删除co??nst,以便我可以把这个函数类型放入std :: function. 我在使用lambdas的operator()方法时遇到这个问题,但我认为这个问题一般是关于任何方法类型,而不仅仅是为了lambdas 但是我的第二个问题是:double(float)const甚至意味着什么?我能够了解

double (ClassName::) (float) const

因为它意味着成员函数不能修改其ClassName对象.当我把这个类型放入一个模板来删除类的类型时,我得到了double(float)const这是造成麻烦的.

template<typename>
struct DropClassType;
template<typename Sig,typename C>
struct DropClassType<Sig (C::*)> {
  typedef Sig type_without_class;
};

(俚语3.4.2.从g -4.9.1的错误更加隐蔽,但基本相同)

解决方法

Why did I get the “implicit instantiation of undefined template” error?

std :: function定义为未定义的基本模板和匹配“常规”函数类型的部分专业化(§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 …),所以你得到了未定义的基本模板.

How to fix this and remove const so that I can put this function type into std::function?

标准的部分专业技巧.当我们在这里,我们也删除挥发性.

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...); };

当然,类似的技巧可以用来删除参考限定词.

What does double (float) const even mean ?!!

这是标准的一个相当模糊的角落(§8.3.5[dcl.fct] / p6)):

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:

  • 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).

[ Example:

06002

end example ]

简而言之,它基本上是“一半”类型,可以用来声明一个类成员函数或一个指向成员的类型(或作为模板参数传递).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读