草庐IT

non-function

全部标签

c++ - 将 std::function 绑定(bind)到不同对象实例的相同函数

是否可以重新绑定(bind)std::function以指向相同的函数但具有不同的对象实例?如果我有一个对象,它有一个绑定(bind)到另一个函数的std::function,但是如果那个对象被复制到另一个实例,我想将std::function重新绑定(bind)到那个新实例而不是旧实例。#include"stdafx.h"#include#includeclassEventHandler{public:intNum;std::functionOnEvent;EventHandler(intinNum){Num=inNum;}EventHandler(constEventHandler

C++ 原子 : would function call act as memory barrier?

我正在阅读这篇文章MemoryOrderingatCompileTime从中说:Infact,themajorityoffunctioncallsactascompilerbarriers,whethertheycontaintheirowncompilerbarrierornot.Thisexcludesinlinefunctions,functionsdeclaredwiththepureattribute,andcaseswherelink-timecodegenerationisused.Otherthanthosecases,acalltoanexternalfunction

c++ - 使用 protected 非虚拟析构函数时抑制 delete-non-virtual-dtor 警告

我有一个纯抽象接口(interface)类和一个实现该接口(interface)的派生类。structFoo{virtualvoiddoStuff()=0;};structBar:Foo{voiddoStuff()override{}};我的接口(interface)类没有虚拟析构函数。因此,尝试使用基类指针破坏派生实例显然是未定义的行为intmain(){Foo*f=newBar;f->doStuff();deletef;}幸运的是我的编译器足够聪明,可以捕捉到这个(使用-Werror)main.cc:15:9:error:deletingobjectofabstractclasst

c++ - 将整数类型转换为枚举 : functional cast vs initialization

假设有一个这样的枚举:enumfoo:int{first,second}然后我使用它如下:foof(1);//error:cannotinitializeavariableoftype'foo'withanrvalueoftype'int'foof=foo(1);//OK!我想知道这两者有什么区别?我知道第二个版本可以看作是函数式转换,但为什么这会有什么不同?例如,如果我这样做:classBar{};Barb=Bar(1);//nomatchingconversionforfunctional-stylecastfrom'int'to'Bar'我显然得到了一个有意义的错误。因此,这让我

C++17 <functional> 模板参数推导不适用于 Xcode 10.1

我一直在尝试使用C++17(及更高版本)进行模板参数推导,并试图从cppreference.com编译这个确切的示例#includeintfunc(double){return0;}intmain(){std::functionf{func};//guide#1deducesfunctioninti=5;std::functiong=[&](double){returni;};//guide#2deducesfunction}它在该页面上的基于Web的编译器中编译并运行良好,但是当我尝试在我的MacbookPro上编译它时,它失败了,说error:noviableconstructor

c++ - 错误 "lambda is not derived from ' std::function'

我正在尝试将lambda传递给通过可变参数模板定义的std::function,但似乎这在gcc上不起作用。有什么原因,为什么这段代码在gcc7.4.0上不起作用,但在VisualStudio2017上却能正常工作?有没有办法让它在gcc上也能工作,而无需先手动将其转换为std::function?#includetemplateintTestFunction(std::function){return0;}voidTest(){autofce=[](int/*n*/,double/*d*/){};//Thisdoesn'tworkwitherrornomatchingfunction

C++ Eclipse 调试器 : "Cannot find bounds of current function" and doesn't stop at breakpoints

我是第一次使用Ubuntu,而eclipse的调试器给我带来的麻烦超出了我的处理能力。目前我只想弄清楚如何让“无法找到当前函数的边界”停止,这样我就可以看到我的控制流在哪里出错了。我知道这是一个模糊的问题,但我愿意快速提供任何类型的必要信息。我在谷歌上搜索了大约2个小时的信息,打开和关闭不同的东西都无济于事。我正在使用版本:3.4.1(我相信是最新的)另外,我的断点并不总是有效(成功率可能约为25%),即使我在构建之前设置它们也是如此。在程序崩溃之前,我的cout如果有任何帮助,我将不胜感激。我会在附近。 最佳答案 不是unhear

c++ - 将 lambda 转换为 std::tr1::function

使用带有tr1服务包和IntelC++编译器11.1.071[IA-32]的visualstudio2008,这与我的其他相关question我正在尝试为c++编写一个功能映射,它的工作方式有点像ruby​​版本strings=[2,4].map{|e|e.to_s}所以我在VlcFunctional命名空间中定义了以下函数templatevectormap(constContainer&container,std::tr1::functionf){vectortransformedValues(container.size());intindex=-1;BOOST_FOREACH(c

c++ - 我怎样才能让 boost::function 不那么宽松?

typedefboost::functionMyCallback;voidRegisterCallback(MyCallbackcallback);classA{public:voidGoodCallback(intintArg,boolboolArg){printf("callingGoodCallback(%d,%s)\n",intArg,boolArg?"true":"false");}voidBadCallback(intintArg){printf("callingBadCallback(%d)\n",intArg);}};intTestFunction(){A*myA=ne

c++ - _wtoi 返回零 : input zero or non-numerical input?

_wtoi当不能转换输入,所以输入不是整数时,返回零。但同时输入可以为零。这是一种确定输入是否错误或为零的方法吗? 最佳答案 这是C++,您应该使用stringstream进行转换:#include#includeintmain(){usingnamespacestd;strings="1234";stringstreamss;ss>i;if(ss.fail()){throwsomeWeirdException;}coutboost的lexical_cast有一个更简洁、更简单的解决方案:#include//...std::stri