草庐IT

default_options

全部标签

C++1 1's "default”只能应用于特殊成员函数?

=default是否只适用于特殊的成员函数?我尝试了以下但它没有编译:structA{A(int,char)=default;inti;charc;}; 最佳答案 是的,您只能显式默认特殊成员函数。来自[dcl.fct.def.default]:Afunctionthatisexplicitlydefaultedshall(1.1)—beaspecialmemberfunction,毕竟,只有特殊的成员函数是隐式默认的——所以为什么显式默认其他任何东西有意义?在这种情况下,您可以简单地删除构造函数并使A成为聚合。这将允许您使用列表初

c++ - 最烦人的解析 : why doesn't `g( ( f() ) );` call `f` 's default constructor and pass the result to `g` 's ctor that takes a `f` ?

这不是Mostvexingparse:whydoesn'tAa(());work?的拷贝,它基于Aa());形式的解析,其OP认为可以使用额外的集合默认构造一个A对象括号。相比之下,我的问题是关于2个类,f和g,其中f具有默认构造函数,而g的构造函数采用f。我想用一个临时的f参数调用g的构造函数,而不使用统一的初始化语法。g的构造函数中有一个std::cout语句,因此缺少输出表示函数声明而不是g对象实例化。我在注释中用3个数字注释了示例代码。#1和#2编译时#3被注释掉,反之亦然:#includestructf{};structg{g(f){std::cout#1:我认为#1声明了一

c++ - 有类似 "default comparator"的东西吗?

我写了一个模板,它包装了一个std::vector以确保vector总是排序:templateclassSortedVector{public:SortedVector(bool(*comparator)(T,T)=DefaultComparator){this->comparator=comparator;}voidinsertValue(TnewElement){vect.insert(std::lower_bound(vect.begin(),vect.end(),newElement,comparator),newElement);}private:std::vectorvec

c++ - 'default_random_engine' 不是 std 的成员

我发现了很多关于这个主题的问题,但所有问题似乎都与不使用C++11编译有关。我的代码是#includeintmain(intargc,char*argv[]){std::default_random_enginegenerator;return0;}即使我用编译gcc-std=c++0xtestmain.cpp给出default_random_engine不是std成员的错误。该程序是在远程机器上编译的,我自己不维护,但gcc-v生成4.4.7版本。有什么想法吗? 最佳答案 对于其他人:检查您是否真的在#include中包含了随机数

c++ - 使用 boost::program_options 禁止无符号值的负参数

假设我有一个程序使用boost::program_options来解析命令行参数,其中一个有一个unsigned值:#include#includenamespacepo=boost::program_options;intmain(intargc,char*argv[]){unsignednum;po::options_descriptiondesc;desc.add_options()("num,n",po::value(&num),"Non-negativenumber");po::variables_mapvm;po::store(po::parse_command_line(

c++ - std::experimental::optional inside constexpr 函数

我想在我的constexpr函数中使用可选的习惯用法来轻松地阐明变量是否已设置。我对std::experimental::optional的尝试:constexprboolcall(){std::experimental::optionalr;r=true;//Error//Similarerrorwith://r=std::experimental::optional(true);if(!r){returnfalse;}return*r;}我得到错误:调用非constexpr函数-所以赋值是不可能的,因为这个操作不能是constexpr(Example)。但如果我实现自己的(非常丑陋

c++ - 在 C++ 中一般将 "optional"字段封装在结构中的最佳方法?

我有很多具体结构,我想将字段指定为可选(存在或不存在)。只是想知道人们对实现这一目标有什么想法。这是一个示例结构(字段也可以是其他结构,甚至是结构vector):structLogonMessage_t{Header_theader;//thispointstoanotherstructcontainingallprimitivesstd::stringusername;std::stringpassword;std::vectorLogonOptions;intsubaccountid;std::stringText;}我想将所有字段默认设置为不存在并一一启用它们,也许在它们的set

C++ "No appropriate default constructor available"

我想在不使用STL的情况下创建一个数组链表。但是,我在将数组传递到我的链接列表时遇到困难...编译时出现上面列出的错误。我需要如何将数组传递给链表?谢谢!(有问题的代码有**标记,如果测试请去掉)单链表.h#pragmaonce#ifndefSinglyLinkedList_h#defineSinglyLinkedList_h#includetemplatestructnode{Typevalue;node*next;};templateclassSinglyLinkedList{private:node*head;public:SinglyLinkedList();~SinglyLi

c++ - boost::optional<std::string> 和来自 char[] 的隐式构造函数

我已经习惯了通过让编译器找出所涉及的魔法来以下列方式初始化std::stringsstd::stringmy_string="hello";以下将不起作用,因为两种类型之间没有显式转换:boost::optionalmy_optional_string="hello";但这确实有效:boost::optionalmy_optional_string=std::string("hello");现在,难道没有办法菊花链隐式调用的单参数构造函数以允许第二种形式吗?我问的原因(虽然我不想用细节打扰你)是有一大堆类需要填充可选成员。必须显式输入所有内容似乎是一种负担(我不太担心自己,但我正在开发

c++ - "except that a default constructed array is not empty"是什么意思?

在N3337中,我正在阅读§23.3.2.1/3,它指出:Anarraysatisfiesalloftherequirementsofacontainerandofareversiblecontainer(23.2),exceptthatadefaultconstructedarrayobjectisnotemptyandthatswapdoesnothaveconstantcomplexity.在§23.2.1,表96容器要求中,它显示了一个默认构造的对象Xu;,其中后置条件是u.empty()。据推测,以下内容:std::arraya;应该导致a.empty()输出1,它确实如此。