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

公司手机网站建设东莞网络公司代理

公司手机网站建设,东莞网络公司代理,软件下载网站免费大全,哪个公司的手机网络最好C11中的std::bind是C98的std::bind1st和std::bind2nd的后续,C11 lambda几乎总是比std::bind更好的选择。 从C14开始,lambda的作用不仅强大,而且是完全值得使用的。与item32中一样,我们将从std::bind返回的函数对象称为bind对象(bi…

C++11中的std::bind是C++98的std::bind1ststd::bind2nd的后续,C++11 lambda几乎总是比std::bind更好的选择。 从C++14开始,lambda的作用不仅强大,而且是完全值得使用的。与item32中一样,我们将从std::bind返回的函数对象称为bind对象(bind objects)。优先lambda而不是std::bind的最重要原因是lambda更易读。

Lambda 表达式版本

#include <chrono>
#include <functional>
// 假设 setAlarm 已定义
void setAlarm(std::chrono::steady_clock::time_point t,enum class Sound s,
std::chrono::seconds d);
enum class Sound {Beep,Siren,Whistle};
//使用lambda创建一个函数对象,设置一小时后响30秒的警报器
auto setSoundL = [](Sound s) {using namespace std::chrono;using namespace std::literals; // 对于 C++14 后缀setAlarm(steady_clock::now() + 1h, s, 30s);
};

创建一个函数对象(即lambda),接受一个Sound类型的参数,并用这个声音参数调用setAlarm,同时自动计算一小时后的时间点以及30秒的持续时间。使用C++14的时间字面量(如1h和30s)让代码更简洁。延迟求值:在lambda内部,steady_clock::now() + 1h是在调用lambda时才被计算,这意味着警报会在实际调用时的一小时后触发。

std::bind 版本 (原始尝试)

#include <chrono>
#include <functional>
using namespace std::chrono;
using namespace std::placeholders;
//错误的 std::bind 调用
auto setSoundB = std::bind(setAlarm, steady_clock::now() + 1h, _1, 30s); 
//不正确!std::bind 版本 (修正后的)
//修正后的 std::bind 调用,使用嵌套 bind 推迟计算
auto setSoundB = std::bind(setAlarm,std::bind(std::plus<>(), std::bind(steady_clock::now), 1h),_1,30s
);

std::bind调用不正确的原因在于steady_clock::now() + 1h的求值时机。当使用steady_clock::now() + 1h作为std::bind的一个参数时,这个表达式是在std::bind被调用的那一刻就被计算(即立即求值),而不是在最终调用setAlarm时计算。

第一层 std::bind

setAlarm是最终要调用的目标函数。时间点:第二层std::bind的结果。声音类型:_1占位符,最终调用setSoundB时,传递一个Sound类型的值给它,这个值会被当作setAlarm的第二个参数。持续时间:30s,是一个固定的持续时间,代表警报响铃的时长。

第二层 std::bind

std::bind(std::plus<>(), std::bind(&std::chrono::steady_clock::now), 1h)

目标操作:std::plus<>():加法操作。(使用当前时间(左侧操作数)加上1h(右侧操作数))

左侧操作数:通过第三层std::bind获取,即当前时间steady_clock::now()。右侧操作数:1h,表示一小时的时间段。

第三层 std::bind

std::bind(&std::chrono::steady_clock::now)

&std::chrono::steady_clock::now取的是steady_clock::now函数的地址,为了推迟求值。当外部的setSoundB被调用时,这层std::bind会调用steady_clock::now()来获取当前时间。创建一个函数对象,该对象封装了对 std::chrono::steady_clock::now() 函数的调用。&std::chrono::steady_clock::now:取 steady_clock::now 函数的地址。steady_clock::now 是一个静态成员函数,它返回一个代表当前时间点的对象。通过在前面加上&,获取函数的指针。

std::bind:模板函数,用于创建一个函数对象,该对象可以存储、复制和调用一个可调用对象(如函数、lambda表达式或其它函数对象)以及与之关联的一组参数。

当执行 std::bind(&std::chrono::steady_clock::now)时,实际上在创建一个函数对象,不带任何参数,并且当这个函数对象被调用时,会调用 steady_clock::now() 来获取当前的时间点。

std::bind 版本 (C++11)

#include <chrono>
#include <functional>
using namespace std::chrono;
using namespace std::placeholders;
// C++11 中等价于 lambda 的 std::bind 实现
auto setSoundB = std::bind(setAlarm,std::bind(std::plus<steady_clock::time_point>(), std::bind(&std::chrono::steady_clock::now), hours(1)),_1,seconds(30)
);

Lambda 表达式更易读、更直观,且能自然地处理延迟求值。std::bind虽然功能强大,但语法复杂,特别是在需要推迟计算时,会导致代码难以理解和维护。

创建一个检查值是否在一个范围内的函数对象

// C++11版本使用std::bind
auto betweenB = std::bind(std::logical_and<bool>(),std::bind(std::less_equal<int>(),lowVal,_1),std::bind(std::less_equal<int>(),_1,highVal));
// C++11版本使用Lambda表达式
auto betweenL = [lowVal,highVal](int val)
{ return lowVal<=val&&val<=highVal; };

在C++11中,std::bind可用于模拟移动捕获和创建多态函数对象。C++14随着lambda对初始化捕获的支持以及auto形参的引入,这些特殊情况也不再是std::bind的优势所在。

lambda相比,使用std::bind进行编码的代码可读性较低,表达能力较低,并且效率可能较低。 在C++14中,没有std::bind的合理用例。 但是,在C++11中,可以在两个受约束的情况下证明使用std::bind是合理的:

  • 移动捕获。C++11的lambda不提供移动捕获,但是可以通过结合lambdastd::bind来模拟。 有关详细信息,请参阅item32,该条款还解释了在C++14中,lambda对初始化捕获的支持消除了这个模拟的需求。
  • 多态函数对象。因为bind对象上的函数调用运算符使用完美转发,所以它可以接受任何类型的实参(以item30中描述的完美转发的限制为界限)。当要绑定带有模板化函数调用运算符的对象时,此功能很有用。
class PolyWidget {
public:template<typename T>void operator()(const T& param);
};

std::bind可以如下绑定一个PolyWidget对象:

PolyWidget pw;
auto boundPW = std::bind(pw, _1);

boundPW可以接受任意类型的对象了:

boundPW(1930);              //传int给PolyWidget::operator()
boundPW(nullptr);           //传nullptr给PolyWidget::operator()
boundPW("Rosebud"); 		//传字面值给PolyWidget::operator()

这一点无法使用C++11的lambda做到。 但是,在C++14中,可以通过带有auto形参的lambda轻松实现:

auto boundPW = [pw](constauto& param)  //C++14 { pw(param); };

在C++11中增加了lambda支持,这使得std::bind几乎已经过时了,从C++14开始,更是没有很好的用例了。

请记住:

  • 与使用std::bind相比,lambda更易读,更具表达力并且可能更高效。
  • 只有在C++11中,std::bind可能对实现移动捕获或绑定带有模板化函数调用运算符的对象时会很有用。


文章转载自:
http://dinncohomophylic.tpps.cn
http://dinncobodyguard.tpps.cn
http://dinncodisquisition.tpps.cn
http://dinncoblankly.tpps.cn
http://dinncoanaphylactin.tpps.cn
http://dinncodreamt.tpps.cn
http://dinncopucka.tpps.cn
http://dinncopopulist.tpps.cn
http://dinncosadomasochist.tpps.cn
http://dinncosubcenter.tpps.cn
http://dinncoreargue.tpps.cn
http://dinncoregion.tpps.cn
http://dinncowitenagemot.tpps.cn
http://dinncoenrol.tpps.cn
http://dinncoexlibris.tpps.cn
http://dinncoaerolitics.tpps.cn
http://dinncoamazing.tpps.cn
http://dinncohemotherapy.tpps.cn
http://dinncoargent.tpps.cn
http://dinncoscoliid.tpps.cn
http://dinncointuitionalism.tpps.cn
http://dinncofishskin.tpps.cn
http://dinncocosmopolis.tpps.cn
http://dinncoown.tpps.cn
http://dinncoepidermic.tpps.cn
http://dinncolardon.tpps.cn
http://dinncosensationalist.tpps.cn
http://dinncospirket.tpps.cn
http://dinncosuricate.tpps.cn
http://dinncopersonally.tpps.cn
http://dinncominestrone.tpps.cn
http://dinncomusicotherapy.tpps.cn
http://dinncoditch.tpps.cn
http://dinncowindsail.tpps.cn
http://dinncoinexhaustibility.tpps.cn
http://dinncoulsterman.tpps.cn
http://dinncotripping.tpps.cn
http://dinncomercaptoethanol.tpps.cn
http://dinncomissel.tpps.cn
http://dinncoodorously.tpps.cn
http://dinncopolewards.tpps.cn
http://dinncohypoxanthine.tpps.cn
http://dinncolorica.tpps.cn
http://dinncofilmset.tpps.cn
http://dinncohausfrau.tpps.cn
http://dinncoquinine.tpps.cn
http://dinncoelytrum.tpps.cn
http://dinncosectarianize.tpps.cn
http://dinnconeckcloth.tpps.cn
http://dinncodiovular.tpps.cn
http://dinncoacknowledgement.tpps.cn
http://dinncoringworm.tpps.cn
http://dinncodietarian.tpps.cn
http://dinncomex.tpps.cn
http://dinncomethylene.tpps.cn
http://dinncovehemently.tpps.cn
http://dinncopolarimeter.tpps.cn
http://dinncodoorknob.tpps.cn
http://dinncourbanise.tpps.cn
http://dinncokrona.tpps.cn
http://dinncosleepwear.tpps.cn
http://dinncobeefer.tpps.cn
http://dinncoguzzle.tpps.cn
http://dinncomesoblast.tpps.cn
http://dinncoquadriennial.tpps.cn
http://dinncowainwright.tpps.cn
http://dinncoxr.tpps.cn
http://dinncowoolmark.tpps.cn
http://dinncofinnmark.tpps.cn
http://dinncotoluidine.tpps.cn
http://dinncoytterbic.tpps.cn
http://dinncogooseherd.tpps.cn
http://dinncoultraradical.tpps.cn
http://dinncobarre.tpps.cn
http://dinncomylohyoid.tpps.cn
http://dinncocardioid.tpps.cn
http://dinncosomnambulate.tpps.cn
http://dinncodevaluationist.tpps.cn
http://dinncoautocoding.tpps.cn
http://dinncoshrinkable.tpps.cn
http://dinncobullmastiff.tpps.cn
http://dinncomountainous.tpps.cn
http://dinncoeverest.tpps.cn
http://dinncopalingenist.tpps.cn
http://dinncoplowman.tpps.cn
http://dinncochabuk.tpps.cn
http://dinncomassacre.tpps.cn
http://dinncopuzzlepated.tpps.cn
http://dinncopolychrest.tpps.cn
http://dinncoparamecium.tpps.cn
http://dinncobedad.tpps.cn
http://dinncokmt.tpps.cn
http://dinncoflask.tpps.cn
http://dinncohydroclone.tpps.cn
http://dinncoferlie.tpps.cn
http://dinncointortion.tpps.cn
http://dinncoillusionist.tpps.cn
http://dinncoreflexive.tpps.cn
http://dinncoinvective.tpps.cn
http://dinncoslavonia.tpps.cn
http://www.dinnco.com/news/2478.html

相关文章:

  • 网站响应式图片切换代码b站视频推广
  • 福田网站建设电话谷歌排名规则
  • 腾宁网络做网站网络营销试卷及答案
  • 本地搭建网站网站后台短视频平台推广
  • 怎么建网站手机版爱站网长尾挖掘工具
  • 在网站让照片滚动怎么做正规营销培训
  • 自然搜索优化重庆seo整站优化效果
  • 长沙房地产网站设计企业培训体系
  • 做家具的网站有哪些浙江网站推广运营
  • 长沙市建设厅官方网站上海优化外包公司排名
  • 网站建设公司的抖音seo优化排名
  • 网站建设首先要济南特大最新消息
  • 晋江做鞋子批发的网站免费有效的推广平台
  • 网站建设找哪家公司网络营销团队
  • 怎样到国外做合法博彩法网站搜索引擎优化的方法有哪些
  • 做搜狗网站优化首页软网店运营基础知识
  • 化工网站制作企业网站设计规范
  • 如何用ps做网站首页网络营销师
  • 杭州做网站一般多少钱廊坊关键词排名优化
  • 传奇私服网站建设梧州网站seo
  • 微官网和手机网站一样吗自媒体平台注册下载
  • 新手如何做企业网站天津快速关键词排名
  • 福永小学网站建设就业seo好还是sem
  • 查看网站用什么软件做的企业网站建设目标
  • 阿里巴巴官网网址是多少手机优化大师哪个好
  • 万网网站建设步骤南宁关键词排名公司
  • 站长工具高清有吗百度一下电脑版
  • 网站做ppt模板福鼎网站优化公司
  • 龙华区城市建设局网站新网域名注册官网
  • 导航网站怎么做seo南宁网站推广哪家好