草庐IT

optional_argument

全部标签

c++ - vector 值 boost::program_options 的默认值

我有一个boost::program_options选项,它成功地将所需的输入选项解析为一个vector,但我没有看到如何也给它一个默认值。示例:base.add_options()("vector_value",po::value>(&vecoption)->multitoken(),"description");可以很好地将值读入vecoptions之类的东西base.add_options()("int_value",po::value(&intoption)->default_value(1),"description");也可以正常工作,但尝试为vector参数提供一个默认值

c++ - 对 std::optional 的转发引用构造函数的约束

std::optional截至目前有8个构造函数,列在下面(也在此处http://en.cppreference.com/w/cpp/utility/optional/optional)/*(1)*/constexproptional()noexcept;/*(1)*/constexproptional(std::nullopt_t)noexcept;/*(2)*/constexproptional(constoptional&other);/*(3)*/constexproptional(optional&&other)noexcept(/*seebelow*/);template/

c++ - 如何抛出 std::invalid_argument 错误?

我在C++的Fraction类中有一个重载运算符,它旨在从标准输入中获取整数形式的输入,即1/2或32/4并根据这些值初始化一个Fraction对象。它有效,但我无法捕获错误。//getsinputfromstandardinputintheformof(hopefully)int/intstd::istream&operator>>(std::istream&inputStream,Fraction&frac){intinputNumerator,inputDenominator;charslash;if((std::cin>>inputNumerator>>slash>>input

c++ - 尝试使用 boost::optional 失败

我一直在尝试将boostoptional用于可以返回对象或null的函数,但我无法弄清楚。这是我到目前为止所拥有的。任何有关如何解决此问题的建议都将不胜感激。classMyclass{public:inta;};boost::optionalfunc(inta)//ThiscouldeitherreturnMyClassoranull{boost::optionalvalue;if(a==0){//returnanobjectboost::optionalvalue;value->a=200;}else{returnNULL;}returnvalue;}intmain(intargc,

c++ - 警告 C4244 : 'argument' : conversion from 'double' to 'const int' , 可能丢失数据

我正在定义“*”运算符以使用“NumericArray”类模板。代码如下:templateNumericArrayNumericArray::operator*(constT&factor)const{NumericArraynewArray(Size());for(inti=0;i当我尝试将类型为“int”的“NumericArray”(NumericArray)与“*”运算符一起使用时,当“factor”参数为double时:intArray1=intArray1*2.5;我收到以下编译器警告:warningC4244:'argument':conversionfrom'doubl

C++ 谷歌模拟 SaveArg : how to save a pointer argument

我在保存模拟接收到的指针参数时遇到困难。#defineSIZE_OF_DATAtypedefstruct{uint32_tsomeValue1;uint16_tsomeValue2;//othervalueshere}LargeStruct;classSomeClass{public://assumesendDataisagenericfunctionwheredataisactuallypointertoaLargeStructvoidsendData(constuint8_t*data,constuint16_tsize);}classMockClass:publicSomeCla

c++ - boost::optional 和类型转换

我想知道是否有一种优雅的方式来转换boost::optional到boost::optional什么时候B可以从A构建,尽管是明确的。这有效:#includeclassFoo{inti_;public:explicitFoo(inti):i_(i){}};intmain(){boost::optionali;...//igetsinitializedornotboost::optionalfoo;foo=boost::optional(bool(i),Foo(i.value_or(0/*unusedvalue*/)));return0;}但是需要将一些永远不会被使用的值放在那里似乎很尴

C++/线程 : No instance of constructor "std::thread::thread" > matches the argument list

我在线程方面遇到了一些问题,因为我对它很陌生。我得到一个:noinstanceofconstructor"std::thread::thread"matchestheargumentlistargumenttypesare(void())恰好在std::threadt1(TestPlay);voidCMusicTCPDlg::OnBnClickedBtplaymusic(){std::threadt1(TestPlay);t1.join();}voidCMusicTCPDlg::TestPlay(){if(CFugue::GetMidiOutPortCount()我引用了一些线程页面,

c++ - std::optional<std::reference_wrapper<T>> - 可以吗?

是std::optional>是否符合C++17的标准(或草案)?标准明确指出,引用类型的std::optional格式错误。但它是否包括reference_wrapper? 最佳答案 是的。那没问题。它不包括reference_wrapper因为reference_wapper不是引用类型。只有实际的引用类型是不允许的。 关于c++-std::optional>-可以吗?,我们在StackOverflow上找到一个类似的问题: https://stackov

c++ - 为什么忽略 std::optional 的强制转换运算符?

这段代码#include#includestructfoo{explicitoperatorstd::optional(){returnstd::optional(1);}explicitoperatorint(){return2;}};intmain(){foomy_foo;std::optionalmy_opt(my_foo);std::cout>(my_foo);std::coutproducesthefollowingoutputconstructor:2static_cast:2在Clang4.0.0和MSVC2017(15.3)中。(让我们暂时忽略GCC,因为在这种情况下它