草庐IT

copy-initialization

全部标签

c++ - initializer_list后面 "hidden array"的存储

在C++11标准中有一条关于支持统一初始化的数组的说明:Theimplementationisfreetoallocatethearrayinread-onlymemoryifanexplicitarraywiththesameinitializercouldbesoallocated.GCC/Clang/VS是否利用了这一点?或者每次使用此功能的初始化都受制于堆栈上的额外数据,以及此隐藏数组的额外初始化时间?例如,给定以下示例:voidfunction(){std::vectorvalues={"First","Second"};...上面提到的每个编译器是否会将支持数组存储到与声明

c++ - Visual Studio 2013 C++ 监 window 口 : arrays copy incorrectly to clipboard

我现在已经直接向微软提出了这个问题here,here和here.显然,该问题在VS2015的当前RC中仍然存在(请参阅上面的第二个链接)。在VisualStudio2013中,可以使用类似p,10000的watch查看C数组的多个元素(例如,其中p是double*)。在下面的示例中,我展示了在监window口中看到的此类数组的一部分的屏幕截图,以及与CTRL-C复制和CTRL-V粘贴到文本编辑器中的数组相同的部分。请注意,从元素25开始,复制/粘贴的值与监window口中的值不一致(正确)。[20]1.0945579725021715double[21]0.99979213435791

c++ - 将包含 initializer_list 的参数包扩展到构造函数

我打算使用shared_ptr在即将到来的项目中相当多,所以(不知道std::make_shared)我想写一个可变参数模板函数spnew(...)作为shared_ptr-返回new的替身.一切都很顺利,直到我尝试使用其构造函数包含initializer_list的类型。.当我尝试编译下面的最小示例时,我从GCC4.5.2得到以下信息:Infunction'intmain(int,char**)':toomanyargumentstofunction'std::shared_ptrspnew(Args...)[withT=Example,Args={}]'Infunction'std

c++ - copy_n 还是直到eof?

我如何使用STL算法来做到这一点?std::ifstreamfile(filename);std::vectorbuf;for(autofile_it=std::istreambuf_iterator(file);file_it!=std::istreambuf_iterator()&&buf.size()备注buf.size().例如如果我执行以下操作并且文件小于2048字节,会发生什么情况?std::copy_n(std::istreambuf_iterator(file),2048,std::back_inserter(buf)); 最佳答案

c++ - 关于 std::initializer_list 设计的问题

我对std::initializer_list的设计有一些疑问。我没有在[support.initlist]中找到答案。为什么它有一个显式定义的默认构造函数?为什么这个构造函数不是constexpr?为什么方法size()不是constexpr?为什么没有特征给出initializer_list的大小(比如专门化std::tuple_size)?为什么不能静态访问它的元素(比如特化std::get)?当sizeof应用于initializer_list时会发生什么? 最佳答案 来自C++标准的第18.9节:Anobjectoftyp

c++ - 如何转发 std::initializer_list<T>?

我正在尝试转发一个std::initializer_list但是noknownconversionfrom'std::initializer_list'to'std::initializer_list'这是测试代码#includeclassB{};classA:publicB{};classnot_working{private:voidfun(std::initializer_listp){}public:templatenot_working(std::initializer_listargs){fun(args);}};classworking{private:voidfun(s

c++ - 从 std::copy 和 std::copy_n 中提取输入迭代器

我试图实现一种反序列化方法,该方法采用输入迭代器并执行一系列block读取(使用std::copy和std::copy_n)。像这样(只是一个例子):templateInputItunserialize(InputItit){std::copy_n(it,sizeof(header_type),reinterpret_cast(&header));std::copy_n(it,header.payload_size,std::back_inserter(payload));it=optional.unserialize(it);returnit;}在这种情况下我如何推进输入迭代器以便每

c++ - GCC 4.4.1 是否因不接受 ctor-initializer 中的注入(inject)类名而出现错误?

GCC4.4.1拒绝在ctor-initializer中找到我的injected-class-name:templatestructBase{Base(intx){}};structDerived:Base{Derived():Base(2){}};intmain(){Derivedd;}test2.cpp:Inconstructor"Derived::Derived()":test2.cpp:9:error:class"Derived"doesnothaveanyfieldnamed"Base"test2.cpp:9:error:nomatchingfunctionforcallto

c++ - 用纯虚拟类 copy-and-swap 成语

我正在尝试使用纯虚方法和“copy-and-swap”惯用法来实现虚类,但我遇到了一些问题。代码无法编译,因为我正在类A的赋值运算符中创建实例,其中包含纯虚方法。有没有办法使用纯虚方法和copyandswapidiom?classA{public:A(stringname):m_name(name){m_type="";}A(constA&rec):m_name(rec.m_name),m_type(rec.m_type){}friendvoidswap(A&lhs,A&rhs){std::swap(lhs.m_name,rhs.m_name);std::swap(lhs.m_type

c++ - 右值数据成员初始化 : Aggregate initialization vs constructor

对于下面的代码:#include#include#includeusingnamespacestd;structFoo{stringtag;Foo(stringt):tag(t){cout结果(g++7.1.0):Foo:BarFoo:Baz~Foo:Baz我们可以看到bar成功地延长了临时Foo的生命周期,但是baz没有这样做。两者有什么区别?如何正确实现Baz的构造函数?编辑:实际上VC++2017给出:Foo:Bar~Foo:BarFoo:Baz~Foo:Baz所以我猜整个事情都不可靠。 最佳答案 Baz是一个带有构造函数的类