草庐IT

some_function_returning_an_option

全部标签

c++ - 什么时候适合使用 std::optional

我想知道这是否会被视为std::optional的有效用法。我有一个返回process_id(std::uint32_t值)的函数,使用标准的“std::uint32_t会更有效吗>"如果我​​们找不到目标进程ID或返回std::optional更合适则返回0的函数?例子:std::optionalFindProcessID(std::string_viewprocess){boolfind=false;if(!find)//wefailtofindtheprocess_idandreturnnothing.returnstd::nullopt;elseif(find)return10

c++ - std::tr1::function 和 std::tr1::bind

我在C++类中使用非常复杂的C函数时遇到问题(重写C函数不是一个选项)。C函数:typedefvoid(*integrand)(unsignedndim,constdouble*x,void*fdata,unsignedfdim,double*fval);//Thisone:intadapt_integrate(unsignedfdim,integrandf,void*fdata,unsigneddim,constdouble*xmin,constdouble*xmax,unsignedmaxEval,doublereqAbsError,doublereqRelError,double

c++ - 嵌入式 C++ : Initialization of an array member of a struct within a class, 大小省略

您好,在此先感谢您对以下问题的任何帮助。编辑:我忘了补充一点,这是在无法访问STL功能的嵌入式系统上。我很抱歉遗漏了这条非常重要的信息。这是我第一次广泛使用C++进行编码,所以我忘了提及显而易见的事情。我回来补充这个事实,这个问题已经收到了一些回复。感谢大家这么快的回复!我正在尝试初始化结构的数组成员,该结构又是C++类的公共(public)成员。结构中省略了数组大小。这是一个例子://ClassA.hClassA{public:structStructA{StructBstructs[];};structStructB{//stuff};ClassA();//etc};//Class

c++ - 从结构中调用 Pointer-to-Member-Function 指向的函数

我有一个具有特殊数据结构的类Test。Test类的成员是std::map,其中键是std::string,映射值是struct定义如下:typedefstruct{void(Test::*f)(void)const;}pmf_t;map初始化正常。问题是当我试图调用指向的函数时。我编了一个重现问题的玩具示例。在这里:#include#includeusingnamespacestd;classTest;typedefvoid(Test::*F)(void)const;typedefstruct{Ff;}pmf_t;classTest{public:Test(){pmf_tpmf={&T

c++ - 使用 boost::program_options 解析配置文件

你好,我写了一个类来通过boost::program_options解析配置文件。这是我的(缩短):namespacensProOp=boost::program_options;nsProOp::variables_mapm_variableMap;nsProOp::options_descriptionm_description;//ToaddoptionstothevariableMap,e.g."addOption("money_amount");"templatevoidaddOption(conststd::string&option,conststd::string&he

c++ - clang++ (3.3/Xcode) 中 std::function 的定义在哪里

问题已解决=>看到最后的更新我正在尝试使用std::function但看起来只包括不提供定义。我试图编译以下代码:#includestd::functionf=nullptr;以c++11作为编译选项:%clang++-c-std=c++11t.cc原因:t.cc:3:6:error:notypenamed'function'innamespace'std'std::functionf=nullptr;~~~~~^t.cc:3:14:error:expectedunqualified-idstd::functionf=nullptr;^2errorsgenerated.我错过了什么?我

c++ - C++ 模板是否能够从父类获取 "forward any class function"?

classFoo{public:voidmethodA();};classManagedFoo{FoofooInst;public:voidmethodA(){doSomething();fooInst.methodA();}};现在我想把ManagedFoo做成一个模板,管理任何类而不仅仅是Foo,并且在调用Foo的任何函数之前,先调用doSomething。templateclassManager{_TyManaged_managedInst;voiddoSomething();public:/*Forwardeveryfunctioncalledby_managedInst*//

c++ - 错误 C2731 : 'wWinMain' : function cannot be overloaded

我将一个旧项目从VC6升级到VS2008,现在我得到这个编译错误:errorC2731:'wWinMain':functioncannotbeoverloaded在这些代码行:intAPIENTRY_tWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPSTRlpCmdLine,intnCmdShow)同一项目在VC6下编译良好。 最佳答案 谢谢大家,我终于找到了真正的罪魁祸首,这是一个错字,我使用LPSTRlpCmdLine而不是LPTSTRlpCmdLine。真正的谜团是为什么

c++ - std::function 与函数指针

这个问题在这里已经有了答案:ShouldIusestd::functionorafunctionpointerinC++?(6个答案)关闭7年前。有什么区别吗?“保存/传输”功能的最佳方式是什么?functionfcn=[](intpar){std::cout

c++ - __has_cpp_attribute 不是 'function-like' 宏?

我正在尝试将[[deprecated]]属性引入我的代码库。然而,并不是所有我需要支持的编译器都支持这种语法(在attributestandardizationproposalN2761中描述了标准化之前不同编译器使用的各种方法)。因此,我尝试在此属性中有条件地编译,首先使用__has_cpp_attribute类宏函数(如果可用),如下所示:#ifdefined(__has_cpp_attribute)&&__has_cpp_attribute(deprecated)#defineDEPRECATED(msg)[[deprecated(msg)]]#elifOTHER_COMPILE