草庐IT

return_value_policy

全部标签

c++ - std::promise set_value 和线程安全

我对std::promise::set_value()上的线程安全要求有点困惑。standardsays:Effects:Atomicallystoresthevaluerinthesharedstateandmakesthatstateready但是,它也说promise::set_value()只能用于设置一次值。如果多次调用,则会抛出std::future_error。所以你只能设置一次Promise的值。事实上,std::promise的几乎每个教程、在线代码示例或实际用例都涉及2个线程之间的通信channel,其中一个线程调用std::future::get(),其他线程调用

c++ - return 语句何时需要显式 move ?

在commenttoanotherquestion中JonathanWakely回应我的声明:Youneverneedexplicitmoveforalocalvariablefunctionreturnvalue.It'simplicitmovethere->...neversaynever...Youneedanexplicitmoveifthelocalvariableisnotthesametypeasthereturntype,e.g.std::unique_ptrf(){autop=std::make_unique();p->foo();returnp;},butifthe

c++ - return 语句何时需要显式 move ?

在commenttoanotherquestion中JonathanWakely回应我的声明:Youneverneedexplicitmoveforalocalvariablefunctionreturnvalue.It'simplicitmovethere->...neversaynever...Youneedanexplicitmoveifthelocalvariableisnotthesametypeasthereturntype,e.g.std::unique_ptrf(){autop=std::make_unique();p->foo();returnp;},butifthe

c++警告: enumeration value not handled in switch [-Wswitch]

我正在尝试编译以下代码而没有警告:while(window.pollEvent(event)){switch(event.type){casesf::Event::Closed:window.close();break;casesf::Event::KeyPressed:if(event.key.code==sf::Keyboard::Escape)window.close();if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))particleSystem.fuel(200/**window.getFrameTime()*/);if(

c++警告: enumeration value not handled in switch [-Wswitch]

我正在尝试编译以下代码而没有警告:while(window.pollEvent(event)){switch(event.type){casesf::Event::Closed:window.close();break;casesf::Event::KeyPressed:if(event.key.code==sf::Keyboard::Escape)window.close();if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))particleSystem.fuel(200/**window.getFrameTime()*/);if(

c++ - 没有 "if(...) return ...;"的 "else"是否被认为是好的风格?

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter寻求指导。关闭11年前.这段代码:if(someCondition)returndoSomething();returndoSomethingElse();对比这段代码:if(someCondition)returndoSomething();elsereturndoSomethingElse();本质上,它们是相同的,但是什么是最好的风格/性能/...(当然,如果答案中有

c++ - 没有 "if(...) return ...;"的 "else"是否被认为是好的风格?

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter寻求指导。关闭11年前.这段代码:if(someCondition)returndoSomething();returndoSomethingElse();对比这段代码:if(someCondition)returndoSomething();elsereturndoSomethingElse();本质上,它们是相同的,但是什么是最好的风格/性能/...(当然,如果答案中有

c++ - 有宏 'return' 的值

我正在使用宏,我认为它可以正常工作-#defineCStrNullLastNL(str){char*nl=strrchr(str,'\n');if(nl){*nl=0;}}因此它可以将字符串中的最后一个换行符归零,实际上它用于在fgets保留它时切断换行符。所以,我想知道我是否可以从宏中“返回”一个值,以便可以像这样调用它func(CStrNullLastNL(cstr));或者我必须写一个函数 最佳答案 对于要“返回值”的宏,宏本身必须是一个表达式。您的宏是一个语句block,不能计算为表达式。你真的应该写一个inline函数。它

c++ - 有宏 'return' 的值

我正在使用宏,我认为它可以正常工作-#defineCStrNullLastNL(str){char*nl=strrchr(str,'\n');if(nl){*nl=0;}}因此它可以将字符串中的最后一个换行符归零,实际上它用于在fgets保留它时切断换行符。所以,我想知道我是否可以从宏中“返回”一个值,以便可以像这样调用它func(CStrNullLastNL(cstr));或者我必须写一个函数 最佳答案 对于要“返回值”的宏,宏本身必须是一个表达式。您的宏是一个语句block,不能计算为表达式。你真的应该写一个inline函数。它

c++ - 为什么 back_insert_iterator/front_insert_iterator/insert_iterator 的 value_type/difference_type/pointer/reference 都是 void?

在我的项目中,我想将流拆分为一些给定类型的值,所以我实现了一个模板函数templateTOutputIterSplitSpace(std::istream&IS,TOutputIterresult){TElemelem;while(IS>>elem){*result=elem;++result;}returnresult;}我认为这很尴尬,因为我必须在调用时明确给出TElem的类型。例如,我必须写:std::vectorv;SplitSpace(std::cin,back_inserter(v));//IwanttoittobeSplitSpace(std::cin,back_inse