草庐IT

some_function_returning_an_option

全部标签

c++ - std::function 在堆栈数组中使用时崩溃

在MSVisualC++2010SP1中,此代码崩溃:#include"stdafx.h"#include#include//#includeinta=0;int_tmain(intargc,_TCHAR*argv[]){//thiswayitworks://std::vector>s;//s.push_back([](){a=1;});//s.push_back([](){a=2;intb=a;});std::functions[]={[](){a=1;},[](){a=2;//Problemoccursonlyifthefollowinglineisincluded.Whencom

c++ - 为什么将 T 从外部模板作为默认参数传递给 std::function 会导致编译错误?

我创建了一个模板类,并将T作为默认类型参数传递。但是,这会导致编译失败。任何人都可以解释发生了什么?谢谢!附言。我使用的编译器是VS2012。#includeusingnamespacestd;templatestructdelegate{typedeffunctionfunction_t;function_tf;};intmain(){delegated;return0;}编译器输出:1>.\MicrosoftVisualStudio11.0\VC\include\functional(554):errorC2027:useofundefinedtype'std::_Get_func

c++ - std::vector 的 std::functions 查找

我有一个填充有回调函数的vector,我想在添加之前检查是否已经存在回调函数。我不知道它是否会工作,但到目前为止它甚至无法编译。vector>_callbacks;voidEvent::RegisterCallback(std::functioncallback){if(callback==NULL)return;vector>::iteratorit=std::find(_callbacks.begin(),_callbacks.end(),callback);if(it==_callbacks.end()){_callbacks.push_back(callback);}else{

c++ - std::bind 到 std::function 使用 Clang 崩溃

在将std::bind与std::function组合时,我无法理解一些细微之处。我已将我的问题最小化为以下代码片段:#include#includevoidbar(intx){std::coutf1=std::bind(bar,std::placeholders::_1);//CRASHESwithclang,worksfineinVS2010andVS2012std::functionf2=std::bind(f1,1);f2();return0;}注意到std::function的显式转换(在构建std::function时将auto替换为f2效果很好)。正在创建f2通过复制f1

c++ - "This application has requested the Runtime to terminate it in an unusual way."

当我关闭Qt程序(g++4.4.0)时,出现MicrosoftVisualC++RuntimeLibrary错误“此应用程序已请求运行时以异常方式终止它”。但是当我在调试器中运行它时,我没有收到错误消息。有谁知道如何获取有关崩溃的一些信息?消息框只有一个确定按钮。编辑添加:按照Wimmel的建议,我附加到调试器。有两个线程还活着,ThreadID为1和3。堆栈看起来像这样:LevelFunctionFileLineAddress0VTagOutputC:\Windows\syswow64\user32.dll00x7529438d1VTagOutputC:\Windows\syswow

c++ - boost::program_options 中带参数和不带参数的参数

我编写了一个使用boost::program_options进行命令行解析的小应用程序。如果参数存在,我希望有一些选项可以设置一个值,如果给出了参数但没有参数,则交替打印当前值。所以“设置模式”看起来像:dc-ctl--brightness15和“获取模式”将是:dc-ctl--brightnessbrightness=15问题是,我不知道如何在不捕获此异常的情况下处理第二种情况:error:requiredparameterismissingin'brightness'有没有一种简单的方法可以避免它抛出该错误?一旦参数被解析,它就会发生。 最佳答案

c++ - NMAKE : fatal error U1077: 'cd' :return code '0x2' cl. 可执行程序

我正在尝试从源代码编译QCAD(一个依赖Qt的开源CAD应用程序),以便我可以为msvs2008构建它。我一直在按照此处给出的说明进行操作:http://www.qcad.org/en/component/content/article/78-qcad/111-qcad-compilation-from-sources.我已成功配置和编译Qt4.8.5,并设置了环境变量PATH。我创建了一个新的环境变量QMAKESPEC并将值设置为win-32-msvc2008。我毫无问题地完成了QCAD的配置。但是,在编译大约30分钟后,我遇到了以下错误:NMAKE:fatalerrorU1077:

c++ - boost::program_options "polymorphic"参数

我想使用boost::program_options创建一个可以按如下方式调用的可执行文件:./example--nmax=0,10#nmaxischosenrandomlybetween0and10./example--nmax=9#nmaxissetto9./example#nmaxissettothedefaultvalueof10用最少的代码以类型安全的方式实现这一目标的最佳方法是什么? 最佳答案 Iwouldliketouseboost::program_optionstocreateanexecutablewhichca

c++ - 具有不同参数的 std::functions 的集合

我正在尝试编写一个简单的调度程序,用户代码可以将回调附加到它。每个事件都有一个已知的签名,用户代码需要使用正确的数字和参数类型来调用调度。这是由可变参数管理的。但是,不接受freestandingInt,因为vector的类型不正确。如何使其通用?遵循一个最小的例子voidfreestanding(){std::cout>listeners;}Event;templatevoiddispatch(inteventNr,Args&&...args){for(autolistener:events[eventNr].listeners){std::functionf(std::bind(l

c++ - 为什么 const 右值限定的 std::optional::value() 返回一个 const 右值引用?

std::optional::value()有以下两个重载constexprT&value()&;constexprconstT&value()const&;constexprT&&value()&&;constexprconstT&&value()const&&;返回const右值引用有什么意义?我能想到的唯一原因是让编译器能够帮助捕获以下(真的很奇怪)情况下的未定义行为autor=std::cref(const_cast&&>(std::optional{}).value());如果std::optional::value()返回了一个constT&,那么上面的代码将编译并在时导致