jjzjj

construction

全部标签

c++ - 返回值优化 : ho can I avoid copy construction of huge STL containers.

当我想要一个函数返回一个容器时:vectorfunc(){vectorresult;...returnresult;}按以下方式使用:vectorresult=func();为了避免复制我的容器的开销我经常编写函数,以便它只返回接受一个容器的非常量实例。voidfunc(vector&result){result.clear();...result;}按以下方式使用:vectorresult;func(result);难道我的努力没有意义,因为我可以确定编译器总是使用返回值优化? 最佳答案 没有意义。你提到的RVO类型称为命名RVO

c++ - try catch finally construct - 它是在 C++11 中吗?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:DoesC++support'finally'blocks?(Andwhat'sthis'RAII'Ikeephearingabout?)C++11是否支持try/catch/finally构造?我问是因为我找不到任何关于它的信息。谢谢。

c++ - 为什么 std::is_copy_constructible 的行为不如预期?

#includeintmain(){std::is_constructible_v;//false,asexpected.std::is_copy_constructible_v;//true,NOTasexpected!}根据cppref:IfTisanobjectorreferencetypeandthevariabledefinitionTobj(std::declval()...);iswell-formed,providesthememberconstantvalueequaltotrue.Inallothercases,valueisfalse.std::is_copy_c

c++ - 了解 std::string 的效率

我正在努力学习更多关于C++字符串的知识。考虑constchar*cstring="hello";std::stringstring(cstring);和std::stringstring("hello");假设在应用程序的.data部分存储“hello”,然后将字节复制到堆上的另一个区域,由std::string管理的指针可以访问它们,我是否正确?我怎样才能有效地存储一个非常非常长的字符串?我正在考虑一个从套接字流中读取数据的应用程序。我害怕连接很多次。我可以想象使用一个链表并遍历这个列表。字符串让我害怕太久了!任何链接、提示、解释和更多详细信息都将非常有帮助。

c++ - thread_local成员变量构造

我遇到了thread_local的一些奇怪行为,不确定是我做错了什么还是GCC错误。我有以下最小重现场景:#includeusingnamespacestd;structbar{structfoo{foo(){cerr输出上面的注释行如下所示:main0Ideone取消注释后,它变成了这样:mainfoofoo4242Ideone我是不是漏掉了什么蠢东西?$gcc-vUsingbuilt-inspecs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapperTarget:x86_6

c++ - 为什么 vector 被认为是 nothrow_move_constructible?

vector的移动构造函数的规范是(从标准中复制出来的):vector(vector&&);注意缺少noexcept.但是gcc4.8和Clang3.2都报告了std::is_nothrow_move_constructible>::value返回真(即1):#include#includeintmain(){std::cout>::value造成这种明显差异的原因是什么? 最佳答案 标准允许实现根据方法加强异常规范17.6.5.12Restrictionsonexceptionhandling[res.on.exception.h

c++ - 模板函数 : default construction without copy-constructing in C++

考虑structC{C(){printf("C::C()\n");}C(int){printf("C::C(int)\n");}C(constC&){printf("copy-constructed\n");}};还有一个模板函数templatevoidfoo(){//default-constructatemporaryvariableoftypeT//thisiswhatthequestionisabout.Tt1;//willbeuninitializedfore.g.int,float,...Tt2=T();//willcalldefaultconstructor,thenco

c++ - 如果对象是普通可构造/可破坏的,是否允许 STL 容器跳过调用 allocator::construct 和 allocator::destroy?

问题在标题中。容器是否允许这样做,或者分配器的方法是否保证被调用,即使对象是微不足道的可构造/可破坏的?我确实尝试搜索此内容,但空手而归...但如果重复,请告诉我。 最佳答案 §23.2.1[container.requirements.general]/p3:Forthecomponentsaffectedbythissubclausethatdeclareanallocator_type,objectsstoredinthesecomponentsshallbeconstructedusingtheallocator_trait

c++ - 构造函数被继承时的 std::is_nothrow_constructible

考虑以下两个示例:structA{A()noexcept=default;};structB:A{B()noexcept=default;templateB(T)noexcept{}};structC:A{usingA::A;templateC(T)noexcept{}};和用法:std::cout::value::value::value::value输出是:1101使用的编译器:GCC4.8.1。因此,如果我显式编写默认的B构造函数,(X)会生成1,另一方面,如果默认的C构造函数可用由于继承,(Y)产生0。这是为什么?这是否意味着在使用is_nothrow_constructibl

c++ - "Expected ' (' for function-style cast or type construction"错误是什么意思?

我收到错误“Expected'('forfunction-stylecastortypeconstruction”,我已尽力在线研究此错误的含义,但无法找到导致此错误的任何文档错误。我在StackOverflow上发现的所有相关问题都修复了特定的代码片段,并且没有更笼统地解释导致错误的原因。这些包括Expected'('forfunction-stylecastortypeconstruction答案突出了代码的几个问题。究竟是哪个问题导致了错误尚不清楚。c++Xcodeexpected'('forfunction-stylecastortypeconstruction在主函数中定义函