草庐IT

optional-parameters

全部标签

c++ - 如何脱离 std::experimental::optional?

使用Boost,我可以创建一个可选的就地:boost::optionalwork=boost::in_place(boost::ref(io_service));并通过以下方式解除它:work=boost::none;有了C++14/实验性支持,我可以改为构建一个可选的就地:std::experimental::optionalwork;work.emplace(boost::asio::io_service::work(io_service));但我不知道如何解除它... 最佳答案 work=std::experimental::

c++ - 显式初始化 std::optional 时,我应该使用 nullopt 吗?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭6年前。Improvethisquestion安std::optional可以像这样初始化为脱离状态:std::optionaloi{nullopt};但也像这样:std::optionaloi{};赋值也是如此(oi={}或oi=nullopt)。除了个人喜好/美感之外,这两者之间是否存在差异,使我更喜欢其中之一?还是根本不重要?注意:我问的是我想显式初始化可选的情况,而不是默认初始化它(例如为了强调)。

C++ 风格约定 : Parameter Names within Class Declaration

我是一个相当新的C++程序员,我想听听支持和反对在类声明中命名参数的争论。这是一个例子:Student.h#ifndefSTUDENT_H_#defineSTUDENT_H_#includeusingnamespacestd;classStudent{private:stringname;unsignedintage;floatheight,GPA;public:Student(string,unsignedint,float,float);voidsetAge(unsignedint);};#endif/*STUDENT_H_*/对比#ifndefSTUDENT_H_#defineS

C++ 宏 : manipulating a parameter (specific example)

我需要更换GET("any_name")与Stringstr_any_name=getFunction("any_name");困难的部分是如何去掉引号。可能的?有什么想法吗? 最佳答案 怎么样:#defineUNSAFE_GET(X)Stringstr_##X=getFunction(#X);或者,为了防止嵌套宏问题:#defineSTRINGIFY2(x)#x#defineSTRINGIFY(x)STRINGIFY2(x)#definePASTE2(a,b)a##b#definePASTE(a,b)PASTE2(a,b)#def

C++11 operator""with double parameter

考虑:structstr{};stroperator""_X(longdoubled){returnstr();}这在g++4.7.2Wallstd=c++11下编译得很好但现在如果我给双倍:stroperator""_X(doubled){returnstr();}我收到以下错误消息:main.cpp|3|错误:'stroperator""_X(double)'的参数列表无效问题是什么?这与“无法重新定义内置文字后缀的含义”(StroustrupFAQ)有关吗?您能想出解决方法吗? 最佳答案 Whatistheproblem?问题

c++ - 当模板template-parameter的模板参数为pack expansion时,gcc失败,clang成功

templatestructS{templatestructA{};templatestructB{};templateclass>structC{};};S::C::B>s1;S::C::A>s2;//gcc5.1.0fails,clang3.6.0succeedsintmain(){}你可以在这里测试http://melpon.org/wandbox/permlink/hhy70gO9LMjLq9nU哪个是正确的,gcc还是clang? 最佳答案 这个问题在gcc6.0中已经解决 关

c++ - 在进程间内存中使用 boost::optional 是否安全?

请考虑以下结构:structThingThatWillGoInSharedMemory{boost::optionalopt_value;};我正在使用boost::interprocess来创建共享内存区域。我对boost::optional的理解是它是一个有区别的union,而不是一个可为空的指针。作为一个反例,像std::map和std::vector这样使用堆的东西需要一个显式分配器才能在进程间内存中使用它们,但是boost::optional,我相当确定不使用堆并且等同于写作:structThingThatWillGoInSharedMemory{boolvalue_init

c++ - 如何构建 Boost::program_options

我想使用boost::program_options。安装boost后,我​​认为我必须单独构建program_options(http://www.boost.org/doc/libs/1_43_0/more/getting_started/windows.html)。但我不知道该怎么做。我正在尝试编译C:\ProgramFiles\boost\boost_1_42\libs\program_options\example\first.cpp(http://www.boost.org/doc/libs/1_42_0/doc/html/program_options/tutorial.

c++ - 使用 boost.program_options 处理 '-'

在你说OVERKILL之前,我不在乎。如何让Boost.program_options处理所需的cat选项-?我有//visiblepo::options_descriptionoptions("Options");options.add_options()("-u",po::value(),"Writebytesfromtheinputfiletothestandardoutputwithoutdelayaseachisread.");po::positional_options_descriptionfile_options;file_options.add("file",-1);

c++ - 如何在 Boost::Program_Options 中支持命令行语法 "-DEVICE:iphone"?

Boost::Program_Options的默认语法是“--DEVICEiphone”。如何支持语法“-DEVICE:iphone”或“-DEVICE=iphone”? 最佳答案 Boost.Program_Options有相当多的optionstyles.您似乎想要的特定组合是:command_line_style::long_allow_adjacent|command_line_style::short_allow_adjacent|command_line_style::allow_long_disguise应该将这些选项