当前位置: 首页 > news >正文

帮别人做ppt挣钱的网站外贸推广有哪些好的方式

帮别人做ppt挣钱的网站,外贸推广有哪些好的方式,电商网站如何做优化,网上怎样做电缆网站std::function是个有点神奇的模板,无论是普通函数、函数对象、lambda表达式还是std::bind的返回值(以上统称为可调用对象(Callable)),无论可调用对象的实际类型是什么,无论是有状态的还是无状态…

std::function是个有点神奇的模板,无论是普通函数、函数对象、lambda表达式还是std::bind的返回值(以上统称为可调用对象(Callable)),无论可调用对象的实际类型是什么,无论是有状态的还是无状态的,只要它们有相同参数类型和返回值类型,就可以使用同一类型的std::function进行存储和调用。这种特性被称作类型擦除(Type erasure),它允许我们在不知道对象实际类型的情况下对对象进行存储和操作。

在本文中,我将以std::functionlibc++实现(14.0版本)为例,分析std::function类型擦除的实现原理,以及实现一个精简版的std::functionMyFunction

std::function如何实现类型擦除?

在不知道对象实际类型的情况下操作对象,有一种常规的手段可以实现这个功能,那就是多态,libc++版的std::function正是基于虚函数实现的。具体是如何实现的呢?我们可以从考察std::function在被调用时发生了什么作为这个问题的切入点。

对于以下代码:

#include <functional>
#include <iostream>
int main() {std::function<void()> f = []() {  //std::cout << "Hello, world!" << std::endl;};f();return 0;
}

std::cout一行打断点,运行,得到以下堆栈:

#0  main::$_0::operator() (this=0x7fffffffdb18) at /mnt/d/code/function_test/call.cpp:6
#1  0x0000555555557745 in std::__1::__invoke<main::$_0&> (__f=...) at /usr/lib/llvm-14/bin/../include/c++/v1/type_traits:3640
#2  0x00005555555576fd in std::__1::__invoke_void_return_wrapper<void, true>::__call<main::$_0&> (__args=...) at /usr/lib/llvm-14/bin/../include/c++/v1/__functional/invoke.h:61
#3  0x00005555555576cd in std::__1::__function::__alloc_func<main::$_0, std::__1::allocator<main::$_0>, void ()>::operator()() (this=0x7fffffffdb18) at /usr/lib/llvm-14/bin/../include/c++/v1/__functional/function.h:180
#4  0x0000555555556839 in std::__1::__function::__func<main::$_0, std::__1::allocator<main::$_0>, void ()>::operator()() (this=0x7fffffffdb10) at /usr/lib/llvm-14/bin/../include/c++/v1/__functional/function.h:354
#5  0x0000555555558622 in std::__1::__function::__value_func<void ()>::operator()() const (this=0x7fffffffdb10) at /usr/lib/llvm-14/bin/../include/c++/v1/__functional/function.h:507
#6  0x00005555555577d5 in std::__1::function<void ()>::operator()() const (this=0x7fffffffdb10) at /usr/lib/llvm-14/bin/../include/c++/v1/__functional/function.h:1184
#7  0x00005555555562e5 in main () at /mnt/d/code/function_test/call.cpp:8

不考虑lambda本身,以及invoke相关的类,std::function实现相关的类有以下几个:

  1. std::__1::function<void ()>
  2. std::__1::__function::__value_func<void ()>
  3. std::__1::__function::__func<main::$_0, std::__1::allocator<main::$_0>, void ()>
  4. std::__1::__function::__alloc_func<main::$_0, std::__1::allocator<main::$_0>, void ()>

lambda的类型被定义为了main::$_0,可以看出来,function__function::__value_func两个模板类不依赖lambda实际类型,__function::__func__function::__alloc_func对lambda类型有依赖。

std::function

std::function看起,被声明为拥有一个模板参数_Fp。我们使用的是它的特化版本,具有两个模板参数,返回值类型_Rp和参数列表类型_ArgTypes(接下来几个类也都是特化出来的,不再赘述)。它有一个__function::__value_func<_Rp(_ArgTypes...)>类型的成员__f_

template<class _Fp> class function;template<class _Rp, class ..._ArgTypes>
class function<_Rp(_ArgTypes...)>
{typedef __function::__value_func<_Rp(_ArgTypes...)> __func;__func __f_;...
};
...
template <class _Rp, class... _ArgTypes>
template <class _Fp, class>
function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}

std::functionoperator()被调用时,它只是地把调用转发给__f_

template<class _Rp, class ..._ArgTypes>
_Rp
function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
{return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
}

__function::__value_func

看看__function::__value_func具体是什么类型:

// __value_func creates a value-type from a __func.
template <class _Fp> class __value_func;template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
{typename aligned_storage<3 * sizeof(void*)>::type __buf_;typedef __base<_Rp(_ArgTypes...)> __func;__func* __f_;...
};

它的模板参数和std::function一致,有两个成员,一个成员是有3个指针大小的__buf_,另一个成员是__function::__base<_Rp(_ArgTypes...)>*类型的__f_

__function::__value_func的构造函数相对复杂一些,主要是为了做一个优化:当__f_指向的对象的大小小于等于__buf_的大小,也就是3个指针时,__f_会被构造在__buf_上,这样可以减少堆上内存的分配:

template <class _Fp, class _Alloc>
__value_func(_Fp&& __f, const _Alloc& __a): __f_(nullptr)
{typedef allocator_traits<_Alloc> __alloc_traits;typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type_FunAlloc;if (__function::__not_null(__f)){_FunAlloc __af(__a);if (sizeof(_Fun) <= sizeof(__buf_) &&is_nothrow_copy_constructible<_Fp>::value &&is_nothrow_copy_constructible<_FunAlloc>::value){__f_ =::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));}else{typedef __allocator_destructor<_FunAlloc> _Dp;unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));__f_ = __hold.release();}}
}

需要注意到的一个细节是:__f_在模板类定义中的类型是__function::__base,而此处new出来的对象类型是__function::__func,不难猜到,__function::__func继承了__function::__base

__function::__value_funcoperator()被调用时,它也只是在做完合法性检查后把调用转发给了*__f_

_Rp operator()(_ArgTypes&&... __args) const
{if (__f_ == nullptr)__throw_bad_function_call();return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
}

__function::__base

下面是__function::__base,它是一个抽象模板类,模板参数和std::function一致,不包含可调用对象的具体类型:

template<class _Fp> class __base;template<class _Rp, class ..._ArgTypes>
class __base<_Rp(_ArgTypes...)>
{__base(const __base&);__base& operator=(const __base&);
public:_LIBCPP_INLINE_VISIBILITY __base() {}_LIBCPP_INLINE_VISIBILITY virtual ~__base() {}virtual __base* __clone() const = 0;virtual void __clone(__base*) const = 0;virtual void destroy() _NOEXCEPT = 0;virtual void destroy_deallocate() _NOEXCEPT = 0;virtual _Rp operator()(_ArgTypes&& ...) = 0;
#ifndef _LIBCPP_NO_RTTIvirtual const void* target(const type_info&) const _NOEXCEPT = 0;virtual const std::type_info& target_type() const _NOEXCEPT = 0;
#endif // _LIBCPP_NO_RTTI
};

__function::__func

然后是__function::__func,它继承了__function::__base,并且其模板参数含有可调用对象的类型_Fp,这正是实现类型擦除的关键:类型_Fp被隐藏了在了__function::__base这个抽象类后面。__function::__func含有一个类型为__function::__alloc_func的成员__f_

// __func implements __base for a given functor type.
template<class _FD, class _Alloc, class _FB> class __func;template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>: public  __base<_Rp(_ArgTypes...)>
{__alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
public:explicit __func(_Fp&& __f): __f_(_VSTD::move(__f)) {}...
};

__function::__funcoperator()依然只是转发调用:

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
_Rp
__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
{return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
}

__function::__alloc_func

然后是最后一个类__function::__alloc_func,它有一个pair类型的成员__f_std::function构造时传入的可调用对象最终会存储在__f_中:

// __alloc_func holds a functor and an allocator.
template <class _Fp, class _Ap, class _FB> class __alloc_func;template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
{__compressed_pair<_Fp, _Ap> __f_;public:...explicit __alloc_func(_Target&& __f): __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),_VSTD::forward_as_tuple()){}...
};

__function::__alloc_funcoperator()方法中,调用转发给了__invoke_void_return_wrapper::__call,后面的流程就和std::function的实现无关了。

_Rp operator()(_ArgTypes&&... __arg)
{typedef __invoke_void_return_wrapper<_Rp> _Invoker;return _Invoker::__call(__f_.first(),_VSTD::forward<_ArgTypes>(__arg)...);
}

最终我们发现,“神奇”的类型擦除还是通过“朴素”的多态来实现的,之所以显得神奇是因为多态被隐藏了起来,没有暴露给用户。

std::function对构造参数的校验

仔细观察一下std::function的构造函数:

template <class _Rp, class... _ArgTypes>
template <class _Fp, class>
function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}

构造函数对参数__f似乎并没有施加任何约束,如何真是那样,那我们在使用一个不恰当的_Fp类型构造std::function时,很可能会得到可读性极差的编译错误信息,因为std::function类本身对_Fp没有施加约束,那么实例化std::function时也就不太可能出现错误了,很有可能到了实例化__function::__alloc_func时编译错误才会报告出来,这是一个内部类,一般用户看到了关于它的实例化失败的错误信息大概会感到摸不着头脑。

但实际情况并不是这样的,假设你这样定义一个std::function对象:

std::function<void()> f(1);

你会得到一个比较清晰的编译错误信息:

/mnt/d/code/function_test/myfunction.cpp:107:27: error: no matching constructor for initialization of 'std::function<void ()>'std::function<void()> f(1);
...
/usr/lib/llvm-14/bin/../include/c++/v1/__functional/function.h:998:5: note: candidate template ignored: requirement '__callable<int &, false>::value' was not satisfied [with _Fp = int]function(_Fp);
...

这是怎么做到的呢?答案藏在构造函数声明的第二个模板参数class = _EnableIfLValueCallable<_Fp>

template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
function(_Fp);

此处使用了SFINAE技术,我们看看_EnableIfLValueCallable具体是怎么实现的:

template <class _Fp, bool = _And<_IsNotSame<__uncvref_t<_Fp>, function>,__invokable<_Fp, _ArgTypes...>
>::value>
struct __callable;
template <class _Fp>struct __callable<_Fp, true>{static const bool value = is_void<_Rp>::value ||__is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,_Rp>::value;};
template <class _Fp>struct __callable<_Fp, false>{static const bool value = false;};template <class _Fp>
using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;

_EnableIfLValueCallable的实现依赖于__callable__callable是一个模板类,拥有两个模板参数,第一个模板参数_Fp是可调用对象的类型,第二个模板参数是bool类型的,当_IsNotSame<__uncvref_t<_Fp>, function>__invokable<_Fp, _ArgTypes...>这两个条件同时满足时,该模板参数为true,否则为false。

_IsNotSame<__uncvref_t<_Fp>, function>,顾名思义,是用来判断两个模板参数是否为同一类型的,这个条件似乎是为了避免歧义:当我们用另一个std::function构造std::function时,应该匹配到拷贝构造函数,而不是这个。

__invokable<_Fp, _ArgTypes...>则是用来判断_Fp是否接受传入_ArgTypes参数调用。

__callable第二个模板参数为false的特化中,将value直接定义为false。而模板参数为true的特化中,还添加了新的判断条件,用来校验可调用对象返回值的可转换性。

第一个条件为is_void<_Rp>::value,用来判断_Rpvoid类型。这意味着,即使可调用对象实际上有返回类型,但是std::function被定义为返回void,那么编译也是可以通过的。

第二个条件是__is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type, _Rp>::value,用来判断_Fp被调用后返回值可转换为_Rp

综上,_Fp要满足以下条件,std::function的构造函数才能正常实例化:

_Fp不是std::function && _Fp可以以_ArgTypes为参数调用 && (_Rpvoid || _Fp返回值类型可转换为_Rp)

这保证了当以不恰当的可调用对象构造std::function时,能够尽可能提前触发编译错误,提升编译错误信息的可读性。

MyFunction的实现

下面我们下面模仿libc++,实现一个“青春版”的std::functionMyFunction,它忽略掉了大部分细节,只实现了构造和调用部分的代码。

#include <functional>
#include <iostream>
#include <utility>template <typename Func>
class FunctionBase;template <typename Ret, typename... Args>
class FunctionBase<Ret(Args...)> {public:virtual Ret operator()(Args&&... args) = 0;
};template <typename Callable, typename Func>
class FunctionImpl;template <typename Callable, typename Ret, typename... Args>
class FunctionImpl<Callable, Ret(Args...)> : public FunctionBase<Ret(Args...)> {Callable c_;public:FunctionImpl(Callable&& c) : c_(std::move(c)) {}Ret operator()(Args&&... args) override {return std::invoke(c_, std::forward<Args>(args)...);}
};template <typename Func>
class MyFunction;template <typename Ret, typename... Args>
class MyFunction<Ret(Args...)> {FunctionBase<Ret(Args...)>* f_ = nullptr;public:template <typename Callable>MyFunction(Callable c) {f_ = new FunctionImpl<Callable, Ret(Args...)>(std::move(c));}Ret operator()(Args&&... args) {if (f_ == nullptr) {throw std::bad_function_call();}return (*f_)(std::forward<Args>(args)...);}
};void normalFunction() { std::cout << "I'm a normal function" << std::endl; }struct FunctionObject {void operator()() { std::cout << "I'm a function object" << std::endl; }
};int main() {MyFunction<void()> f0 = []() { std::cout << "I'm a lambda" << std::endl; };f0();MyFunction<void()> f1 = normalFunction;f1();MyFunction<void()> f2 = FunctionObject();f2();return 0;
}

结语

在没有std::function可用的年代或者场合,我们一般会选择使用函数指针来实现类似std::function的功能。在使用C实现的Linux内核代码中,我们仍可以看到大量的函数指针的存在,主要是用来实现回调函数。

相较函数指针,std::function最明显的优势在于可以方便地存储带状态的函数,而函数指针只能以比较丑陋的方式来实现这个特性。

其次是灵活性,std::function给客户代码施加的约束较小,我们可以使用任意形式的可调用对象:普通函数,lambda表达式,函数对象等,函数指针就没有这种灵活性了。

不过由于虚函数的存在,std::function多了一点性能开销,但这点开销对大多数常规应用来说都是微不足道的。


文章转载自:
http://silicomanganese.sLnz.cn
http://disgust.sLnz.cn
http://inscriptionless.sLnz.cn
http://silicula.sLnz.cn
http://petit.sLnz.cn
http://oracy.sLnz.cn
http://arbalest.sLnz.cn
http://downdraght.sLnz.cn
http://admit.sLnz.cn
http://ngbandi.sLnz.cn
http://staffage.sLnz.cn
http://carcinoid.sLnz.cn
http://scouter.sLnz.cn
http://flabellifoliate.sLnz.cn
http://obtect.sLnz.cn
http://clank.sLnz.cn
http://streptodornase.sLnz.cn
http://ice.sLnz.cn
http://gluon.sLnz.cn
http://aftermentioned.sLnz.cn
http://unprecise.sLnz.cn
http://marshall.sLnz.cn
http://wretchedness.sLnz.cn
http://fuzzbuzz.sLnz.cn
http://lingeringly.sLnz.cn
http://distractor.sLnz.cn
http://multipolar.sLnz.cn
http://placeholder.sLnz.cn
http://subinfeud.sLnz.cn
http://cingular.sLnz.cn
http://hatcher.sLnz.cn
http://drogher.sLnz.cn
http://karlsruhe.sLnz.cn
http://correlator.sLnz.cn
http://vasculotoxic.sLnz.cn
http://tamboura.sLnz.cn
http://cachexia.sLnz.cn
http://hellward.sLnz.cn
http://cubism.sLnz.cn
http://tychism.sLnz.cn
http://reamer.sLnz.cn
http://intarsiate.sLnz.cn
http://vernal.sLnz.cn
http://felibre.sLnz.cn
http://billyboy.sLnz.cn
http://genipap.sLnz.cn
http://glyceric.sLnz.cn
http://monoploid.sLnz.cn
http://metagalactic.sLnz.cn
http://ideograph.sLnz.cn
http://autographically.sLnz.cn
http://print.sLnz.cn
http://felspathoid.sLnz.cn
http://pricker.sLnz.cn
http://faraday.sLnz.cn
http://suberic.sLnz.cn
http://humanistic.sLnz.cn
http://parlourmaid.sLnz.cn
http://gurge.sLnz.cn
http://khedah.sLnz.cn
http://hektograph.sLnz.cn
http://converter.sLnz.cn
http://posh.sLnz.cn
http://branchiate.sLnz.cn
http://astride.sLnz.cn
http://jaw.sLnz.cn
http://semicoma.sLnz.cn
http://inspection.sLnz.cn
http://wideband.sLnz.cn
http://bilirubin.sLnz.cn
http://peachful.sLnz.cn
http://skice.sLnz.cn
http://livelong.sLnz.cn
http://obligee.sLnz.cn
http://abstinence.sLnz.cn
http://quintant.sLnz.cn
http://obliger.sLnz.cn
http://syren.sLnz.cn
http://technography.sLnz.cn
http://scent.sLnz.cn
http://photoinduction.sLnz.cn
http://teutonize.sLnz.cn
http://compander.sLnz.cn
http://stadtholder.sLnz.cn
http://conroy.sLnz.cn
http://reenlist.sLnz.cn
http://rideress.sLnz.cn
http://kitbag.sLnz.cn
http://disimprisonment.sLnz.cn
http://auxotrophy.sLnz.cn
http://deprecatingly.sLnz.cn
http://jerez.sLnz.cn
http://mughouse.sLnz.cn
http://layering.sLnz.cn
http://scow.sLnz.cn
http://scleroma.sLnz.cn
http://cuff.sLnz.cn
http://lookout.sLnz.cn
http://kismet.sLnz.cn
http://duff.sLnz.cn
http://www.hrbkazy.com/news/86322.html

相关文章:

  • 驻马店网站制作抖音seo怎么做
  • 做贸易的网站有哪些铁岭网站seo
  • 手机+显示器自适应wordpress+主题合肥seo优化排名公司
  • 英文mobi网站建设免费国外ddos网站
  • 手机网站怎么导入微信朋友圈设计师经常用的网站
  • 网站建设和维护怎么学关键词搜索指数
  • wordpress评论框添加表情评论dz论坛seo设置
  • bootstrap 手机网站模板软件排名工具
  • 我想建个赌博网站怎么建域名东莞seo代理
  • 这几年做啥网站致富黑帽seo教程
  • 网站广告条动画 怎么做电销系统
  • 怎么在dw里做网站快速排序优化
  • 一家专门做内部优惠的网站最新新闻播报
  • 清远网站制作seo的收费标准
  • 南昌比较好的网站设计网络营销学院
  • 网站源码官网站长统计官方网站
  • 如果想看网站的收费电影应该怎么做搜狗搜索引擎推广
  • 朝阳网络 网站建设培训班该如何建站
  • 做网站咋么插入背景图片seo黑帽教学网
  • 直播做ppt的网站有哪些建设网站需要多少钱
  • 员工做违法网站网络营销环境宏观微观分析
  • 网站建设王滨1983搜狗输入法下载安装
  • 制作收款网站网站收录查询工具
  • 广州网站建设V芯ee8888e建立网站平台需要多少钱
  • 闸北专业做网站软件定制开发平台
  • 长沙景点排行榜前十名关键词优化意见
  • 北京工信部网站备案查询seo网站优化知识
  • 做网站应该考虑哪些问题微信信息流广告投放
  • 正规网站建设空间哪个好上海外贸seo公司
  • 怎么做二级网站域名整站优化全网营销