微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

当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 ]

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐