草庐IT

optional-arguments

全部标签

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

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

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

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

c++ - enable_if : minimal example for void member function with no arguments

我试图更好地理解C++11中的std::enable_if并且一直在尝试编写一个最小的示例:一个类A带有成员函数voidfoo()根据类模板中的类型T具有不同的实现。下面的代码给出了期望的结果,但我还没有完全理解它。为什么版本V2有效,但V1无效?为什么需要“冗余”类型U?#include#includetemplateclassA{public:A(Tx):a_(x){}//EnablethisfunctionifT==int/*V1*///template::value,int>::type=0>/*V2*/template::value,int>::type=0>voidfoo(

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++ - "could not convert template argument"指针参数错误,即使使用强制转换

假设我有一个声明如下的模板类:templatestructy{int*b;y(){b=x;}}我确实需要模板参数是一个常量内存地址——它是一个嵌入式代码。如果我尝试像这样实例化它:(编译器是带有-std=gnu++11的gcc4.8.1)yc;我会收到错误消息“无法将模板参数‘1’转换为‘int*’”,这没关系,而且符合标准。我明白。我的问题是转换为指针也不起作用:yd;y(1)>e;error:couldnotconverttemplateargument'1u'to'int*'在这两种情况下。这是为什么?模板参数已经转换,不是吗? 最佳答案

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应该将这些选项

c++ - experimental::optional nullopt_t 构造函数

Here描述了nullopt_t和nullopt用于为c++提议的optional对象:structnullopt_t{seebelow};constexprnullopt_tnullopt(unspecified);[...]Typenullopt_tshallnothaveadefaultconstructor.Itshallbealiteraltype.Constantnulloptshallbeinitializedwithanargumentofliteraltype.原因在Theop={}syntax中有解释。文档章节:为了使op={}明确,必须采用一些技巧,其中之一是nu

c++ - Boost Program_Options 抛出 "character conversion failed"

我在Ubuntu14.04上,使用CMake和CLion。我正在尝试使用程序选项,以下代码取自其文档中的示例:#include#includeintmain(intac,char*av[]){namespacepo=boost::program_options;usingnamespacestd;po::options_descriptiondesc("Allowedoptions");desc.add_options()("help","producehelpmessage")("compression",po::value(),"setcompressionlevel");po::

c++ - 如何为 boost::program_options 的位置选项添加描述?

我想用boost_program_options创建一个位置列表程序选项,不允许命名程序选项(如--files)。我有以下代码片段:#include#include#include#includenamespacepo=boost::program_options;intmain(intargc,constchar*argv[]){po::options_descriptiondesc("Allowedoptions");desc.add_options()("help","producehelpmessage")("files",po::value>()->required(),"l