草庐IT

function-call-operator

全部标签

c++ std::forward 在容器上调用 operator[]

《EffectivemodernC++》一书中第3条写了这样一段代码:templatedecltype(auto)authAndAccess(Container&&c,Indexi){authenticateUser();returnstd::forward(c)[i];}我不明白你为什么调用std::forward?如果c是右值引用,那么在右值而不是左值上调用operator[]会发生什么变化?对我来说c[i]应该足够了。PS:当变量是函数的参数时,我理解std::forward的目的是:templatestd::unique_ptrmake_unique(Ts&&...params

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++ 销毁顺序 : Calling a field destructor before the class destructor

有没有办法在类析构函数之前调用字段析构函数?假设我有2个类Small和Big,Big包含一个Small的实例作为它的字段因此:classSmall{public:~Small(){std::cout当然,这会在小析构函数之前调用大析构函数:BigdestructorSmalldestructor我需要在Big析构函数之前调用Small析构函数,因为它会为Big析构函数执行一些必要的清理工作。我可以:显式调用small.~Small()析构函数。->但是,这会调用Small析构函数两次:一次显式调用,一次在Big析构函数执行后调用。有一个Small*作为字段并在Big析构函数中调用del

c++ - 显式调用 `operator new` 后无法访问对象的函数

我正在做一个项目,我必须实现newoperator和deleteoperator,并通过我自己的MemoryManager管理我的内存-它有可用内存列表.为了分配我的列表和节点(不需要管理),我应该在调用malloc之后显式调用operatornew。当我尝试调用一个函数-setNext()时,它抛出异常:Exception:EXC_BAD_ACCESS(code=1,address=0x0)创建链表的哈希表:MyHashTable::MyHashTable(size_tmemorySize,void*startingPtr):size(getLowerLog(memorySize)+

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++ - 将 Boost 参数与 operator() 一起使用

我想将BoostParameter与重载调用运算符(operator())一起使用:#include#include#includestructadd_argument_tag{structname_;structdescr_;};staticinlineboost::parameter::keyword&name=boost::parameter::keyword::get();staticinlineboost::parameter::keyword&descr=boost::parameter::keyword::get();structconfig{BOOST_PARAMETE

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