草庐IT

move_function_imp

全部标签

c++ - g++ 链接问题 : In function `_start' : (. text+0x20): undefined reference to `main'

我收到对主要错误的undefinedreference-即使我已经定义了主要,并且(AFAICT),我已经正确链接了它。这是我的代码和我使用的命令://################################################//proj1.h#ifndef__SCRATCH_PROJ1_H#define__SCRATCH_PROJ1_HintaddOne(inti);#endif/*__SCRATCH_PROJ1_H*///################################################//proj1.cpp#include"pr

c++ - 使用 -g 选项编译但 "Single stepping until exit from function main, which has no line number information"

我在使用gdb时遇到了一些问题。这是我在一个名为main.cpp的文件中的代码#includevoidmyfunc();intmain(){charmsg[]="HelloWorld!";myfunc();std::cout我使用这个命令来编译这段代码:g++-g-Wallmain.cpp-ofoo接下来,我使用了gdb:$gdbfoo(gdb)startTemporarybreakpoint1at0x80487c3Startingprogram:/home/laptop/workspace/fooTemporarybreakpoint1,0x080487c3inmain()(gdb)

c++ - std::function 在调用内部可变 lambda 时是否锁定互斥量?

除非另有说明,否则C++标准库对其类型提供以下保证:(1)读取操作(即处理const对象)是线程安全的。这意味着只要没有线程同时写入(应用非const操作)对象,多个线程就可以同时从对象读取而不会出现竞争条件。(2)多个线程可以同时读写任意对象,只要每个对象一次最多只能被一个线程访问。标准库需要用户类型的相同保证。(您可以在GotW#95中阅读这些内容或观看HerbatC++andBeyond2012对此的解释。)现在我的问题是,如果下面的结论是正确的:因为std::function的operator()是一个const成员函数,它需要是线程安全的。如果在构造时传入的仿函数有一个con

c++ - 为什么 list<unique_ptr> 中的 unique_ptr 的 std::move() 没有真正 move 它?

usingPtr=std::unique_ptr;Ptrf(boolarg){std::listlist;Ptrptr(newint(1));list.push_back(std::move(ptr));if(arg){Ptr&&obj1=std::move(list.front());//Here|obj1|and|list.front()|stillpointtothesamelocation!list.pop_front();returnstd::move(obj1);}else{Ptrobj2=std::move(list.front());list.pop_front();r

c++ - 错误 C2448 : function-style initializer appears to be a function definition

我有以下一段代码-voidCommandProcessor::ReplacePortTag((void*)portID){std::stringtemp=std::string.empty();intstart=0;for(inti=0;i"){temp+=CommandProcessor::fileContents.substr(start,i-start);temp+=portID;start=i+6;}}temp+=CommandProcessor::fileContents.substr(start+6,CommandProcessor::fileContents.length

c++ - 在 Visual Studio 中调用 std::swap 时的 std::bad_function_call

我正在尝试将我的代码从Linux移植到Windows。但是,对于VisualStudio,我的代码因以下错误而崩溃:MicrosoftC++exception:std::bad_function_callatmemorylocation这是我的代码:#includeclassFoo{public:Foo(int):m_deleter{[](){}}{}Foo(constFoo&)=delete;Foo(Foo&&)=default;Foo&operator=(constFoo&)=delete;Foo&operator=(Foo&&)=default;~Foo(){m_deleter(

c++ - 为什么 move 语义和 RVO 都没有按预期工作?

我最近在我的方程求解器中偶然发现了一些奇怪的行为,这让我问自己是否真的了解move语义和RVO如何协同工作。有很多relatedquestions在这个论坛上,我也读过manygeneralexplanations对此。但我的问题似乎很具体,所以我希望有人能帮助我。涉及的结构总体上有点复杂,但至少可以分解为:structFoo{Bar*Elements;Foo(void):Elements(nullptr){cout现在让我们考虑以下短程序:intmain(){Fooa=Foo::Example();cout这些是我在运行这段代码之前的期望:Example方法实例化本地Foo对象,导致

c++ - 指向堆栈分配对象和 move 构造的指针

注意:这是对aquestion的完整改写我前一阵子发帖了。如果您发现它们是重复的,请关闭另一个。我的问题很普遍,但似乎可以根据具体的简单示例更容易地解释它。所以想象一下,我想模拟办公室的电力消耗。假设只有灯和暖气。classSimulation{public:Simulation(Timeconst&t,doublelightMaxPower,doubleheatingMaxPower):time(t),light(&time,lightMaxPower),heating(&time,heatingMaxPower){}private:Timetime;//Note:stack-all

c++ - 在成员初始化列表中使用 std::function

我有一个类型定义:typedefS32(iMyDataClass1::*getDataFunction_t)(void);和类型:structfunctionMap_t{std::vectorpDataFunctionTable;structdataRequestor_tdataRequestor;};然后我有一个成员变量:functionMap_tFnMap1;然后我在成员初始值设定项列表中设置:myClass::myClass():FnMap1({{&iMyDataClass1::getData1,&iMyDataClass1::getData2,&iMyDataClass1::g

c++ - 从 VS 2012 中的 lambda 返回值构造 std::function 时崩溃

此C++代码使用VS2012成功编译但在运行时崩溃:#include#includevoidf(){std::coutfilter(get_f());//crashhere!!!return0;}如果我们将get_f更改为:autoget_f=[](){returnf;};然后程序运行没有崩溃。是这段代码的问题还是编译器/标准库的bug?我没有使用较新版本的VisualStudio进行测试。 最佳答案 在我看来,这像是标准库(或可能是编译器)的问题。使用VS2013,它可以毫无问题地编译和运行。如果我们添加代码来调用同样运行的fil