草庐IT

optional-arguments

全部标签

c++ - 默认参数 : can only the last argument(s) be left?

我知道可以做类似的事情:intfoo(inta=0,intb=1){returna+b;}然后在没有默认参数的情况下使用它,例如:foo();//a=0,b=1->1或将最后一个作为默认值,例如:foo(2);//a=2andb=1default->3但是我的问题是:是否可以为第一个参数(a)使用默认值并给出第二个参数(b)的值我的第一个想法是这样做(行不通!):foo(,2);//a=0defaultandb=2这个语法是否存在或者这是不可能的? 最佳答案 不,在当前语法中这是不可能的。

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++ - 使用 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++ 模板复制构造函数,编译器说 "passing const as this argument discards qualifiers"

我正在尝试创建动态矩阵的模板类。凭借我目前对C++的了解,我设法解决了一些问题,但我被复制构造函数和重载operator=;困住了。换句话说,我无法创建对象的拷贝。在我看来这应该可行,但我的编译器friend告诉我有1个错误:错误:将“constMatrix”作为“intMatrix::getElement(int,int)[withT=int]”的“this”参数传递会丢弃此行的限定符[-fpermissive]:m[i][j]=original.getElement(i,j);当我想创建一个对象时:Matrixm=Matrix(3,3);我的模板类在这里:templateclass

c++ - (C++) 错误 : 'invalid_argument' was not declared in this scope

我正在使用EclipseC/C++和MinGW编译器。我已将标志-std=c++11添加到项目属性中C/C++Build下的MiscellaneousGCCCCompilerSettings中。我知道这可能是一件简单的事情,但我无法解决此错误。Date.h#includeusingnamespacestd;classDate{public:Date(intm=1,intd=1,inty=1900);voidsetDate(int,int,int);private:intmonth;intday;intyear;staticconstintdays[];};日期.cpp#include#

c++ - 警告 C4244 : 'argument' : conversion from 'SIZE_T' to 'DWORD' , 可能丢失数据

我需要在我的代码中有一组重载函数,但我得到了转换wanrings。这是一个测试代码:#includewindows.hvoidf(DWORDarg){...}//voidf(SIZE_Targ){}voidmain(void){DWORDdword=0;SIZE_Tsize_t=dword;f(size_t);}编译器给出警告:test.cpp(11):warningC4244:'argument':conversionfrom'SIZE_T'to'DWORD',possiblelossofdata如果我取消注释voidf(SIZE_Targ)我得到test.cpp(5):errorC

【发布选项】Publishing Options

Thesearetheavailablepublishingoptions.这些是可用的发布选项。Ifthejournalacceptsyourarticle,youcanchangeyourmindabouttheseoptions.如果期刊接受您的文章,您可以改变对这些选项的看法。Note:editorsandreviewerscanonlyseeyourselectionafteracceptance.注意:编辑和审稿人只有在接受后才能看到您的选择。GoldOpenAccess金牌开放获取Uponacceptance,myfinalpublishedjournalarticlewillb

c++ - 嵌套命名空间 : where should default template arguments go? 中模板类的前向声明

我在嵌套命名空间中有一个模板类的前向声明namespacen1{namespacen2{templatestructA;}usingn2::A;}接着是一个定义,实际上它在不同的文件中,中间有一些东西:structX{};namespacen1{namespacen2{templatestructA{};}usingn2::A;}那么以下总是可以的:n1::n2::Aa;但是这个捷径n1::Aa;在clang中给出编译错误error:toofewtemplateargumentsforclasstemplate'A'除非我删除前向声明;g++两者都接受。clang似乎保留在第一个不包含

c++ - 为什么 "cc1plus: warning: unrecognized command line option"选项的 "no-"仅在出现另一个警告时由 g++ 标记?

>catwarning.cpp#pragmafoobar>catno_warning.cpp#pragmamessage"foobar">g++-Wall-Wno-foobar-cwarning.cppwarning.cpp:1:0:warning:ignoring#pragmafoobar[-Wunknown-pragmas]cc1plus:warning:unrecognizedcommandlineoption"-Wno-foobar"[enabledbydefault]>g++-Wall-Wno-foobar-cno_warning.cppno_warning.cpp:1:17

c++ - optional<double> 可以实现为 8 字节对象吗?

是否可以实现std::optional这样sizeof(std::optional)==8通过某种方式使用它可以将字符存储在NAN中,请参阅http://en.cppreference.com/w/cpp/numeric/math/nan?有实现吗?可以根据标准中提供的功能来完成吗? 最佳答案 我不认为这是可以做到的,因为没有规则阻止程序自己利用和依赖NaN中的额外位。然后,如果您将魔数(MagicNumber)存储到optional中,它看起来就像不存在,而不是应用程序的特殊NaN。