草庐IT

初始化OpenStack

全部标签

c++ - g++ 4.9 拒绝 C++14 中的有效聚合初始化

考虑这段代码:structS{intx;doubley=1.1;};intmain(){Ss={0};}根据C++14标准,§8.5.1/7Iftherearefewerinitializer-clausesinthelistthantherearemembersintheaggregate,theneachmembernotexplicitlyinitializedshallbeinitializedfromitsbrace-or-equal-initializeror,ifthereisnobrace-or-equal-initializer,fromanemptyinitiali

c++ - 为什么 std::shared_ptr 的别名构造函数不初始化 std::enabled_shared_from_this?

考虑以下代码:structFoo:std::enable_shared_from_this{};structBar{Foofoo;};intmain(){std::shared_ptrbar_p(newBar);//makeshared_ptrtomemberwithaliasingconstructorstd::shared_ptrfoo_p(bar_p,&bar_p->foo);assert(bar_p->foo.shared_from_this());//fail!throwsbad_weak_ptr}不幸的是,它没有按预期工作(至少在GCC4.8.2中)。我查看了代码,似乎别名

c++ - 在初始化构造函数时 {} 或 default 之间是否有任何差异

这三种默认类构造函数的方法之间是否有任何区别(无论多么微小):直接在header中使用{}://foo.hclassfoo{public:foo(){}}直接在header中使用default关键字://foo.hclassfoo{public:foo()=default;}在cpp中使用{}//foo.hclassfoo{public:foo();}//foo.cpp#include"foo.h"foo::foo(){} 最佳答案 是的,有区别。选项1和3是用户提供的。用户提供的构造函数是非平凡的,使类本身非平凡。这对如何处理类有

c++ - 返回值的初始化应该忽略自动对象的常量性

坚持const正确性,您可能希望使您的本地对象类型为unique_ptrconst如下所示,而T这里是一些不重要的类型:unique_ptrfoo(){constunique_ptrp=make_unique(...);...usingp'spointee...returnp;}不幸的是它没有编译,因为返回值无法初始化,因为unique_ptr自p以来没有复制构造函数并且move构造函数不可行是常量。如果C++标准规定当return语句的“操作数”是一个自动对象时,那么它潜在的常量性将被忽略怎么办?返回值初始化后不能引用自动对象,因此它的常量性现在无关紧要。在其析构函数中,也可以修改c

c++ - 使用可变参数模板进行统一初始化

我有一个PODChParam,它是可变参数模板函数set中的一个参数。我想在花括号p.set({Param::D,1000.f},{Param::p,2000.f})中传递给函数参数(构造函数参数)。并认为将隐式调用构造函数并创建ChParam对象。但这是不可能的,我应该明确地创建一个对象a.set(ChParam{Param::D,1000.f},ChParam{Param::p,2000.f});是否有可能以某种方式使用变体p.set({Param::D,1000.f},{Param::p,2000.f})?#includeusingnamespacestd;usingFloat=

c++ - 初始化 std::pair<double, std::array<std::pair<double, double>, 3>>

谁能建议以下代码中std::vector::push_back调用中std::make_pair调用的正确语法:#include#include#includeintmain(){typedefstd::pairPairType;std::vector>>myVector;doubleKey=0.0;PairTypePair1=std::make_pair(1.0,2.0);PairTypePair2=std::make_pair(3.0,4.0);PairTypePair3=std::make_pair(5.0,6.0);myVector.push_back(std::make_pa

c++ - 可变参数模板不适用于初始值设定项列表

我创建了一个工厂函数模板:templatestd::shared_ptrcreate(Args...args){returnstd::make_shared(args...);}还有一个简单的容器:structGroup{std::vectorvec;Group(std::initializer_listil):vec(il){}};然后我尝试创建一个组intmain(){autogr=create({1,2,3});return0;}这不编译,error:nomatchingfunctionforcallto'create'autogr=create({1,2,3});candida

c++ - 为什么在初始化私有(private)静态成员时允许调用私有(private)静态方法?

此代码按我想要的方式编译和工作,但为什么呢?#includeclassTest{private:staticbool_inited;staticbool_init(){std::cout如果我做了一个我认为不相关的改变:bool_inited=Test::_init();它不再编译,给我关于尝试调用私有(private)方法的预期错误。 最佳答案 ThiscodecompilesandworksthewayIwantit,butwhy?因为在使用它的地方,您是在类范围内操作,作为类的一部分。如果你写的话也是一样的:staticvoi

c++17 聚合初始化

我正在观看JasonTuner在youtube上的一个视频系列以及在此处找到的这个特定视频:AggregateInitializations他开始使用clang++c++03,然后将编译器更改为clang++c++11,然后当他合并基类层次结构时,他将其更改为使用c++17或使用最近构建的clang++的c++1z。我最近刚刚下载并安装了MSVS2017RC,我在IntelQuadCoreExtreme上运行Windows764位SP1。然而,当我在我的IDE中尝试观看他的视频时structB{doubleq;};structS:B{inti;floatf;};intmain(){Ss

c++ - 使用 Int 的初始化列表初始化 Long Double 的 vector

假设我有一个简单的类:classPvector{private:std::vectorpoint_list;public:Pvector(std::initializer_listcoords):point_list(coords){}Pvector(std::initializer_listcoords):point_list(coords){}};这将无法编译,因为longdouble模板化的std::vector无法从int类型模板化的初始化列表中初始化自身.然而,这很不方便,因为删除了第二个构造函数后,我无法在我的代码中执行以下操作:Pvectorpiece_movement(