花括号的类型如何影响C++中的对象生存期?

问题描述我的一个朋友给我看了一个C++20的程序:#include<iostream>structA{A(){std::cout<<"A()";}~A(){std::cout<<"~A()";}};structB{constA&a;};intmain(){Bx({});std::

发布:2022-10-16 标签:c++initializationobject-lifetimec++20temporary-objects


在临时对象上调用成员函数时生成警告

问题描述给定矩阵模板类mat<M,N,T>,下面的成员函数允许我高效地转置行向量或列向量,因为它们具有相同/相应的内存占用:template<intM,intN=M,typenameT=double>structmat{//...template<intMd=M,intNd=N,

发布:2022-10-16 标签:c++c++17temporary-objects


非常量引用绑定到临时的 Visual Studio 错误?

问题描述我在gcc中编译一些可移植代码时遇到了这个问题.基本上这个奇怪的代码在VisualStudio中编译,这真的让我大吃一惊:Iranintothiswhilecompilingsomeportablecodeingcc.Basicallythisstrangecodecom

发布:2022-10-16 标签:visual-studioc++temporary-objects


临时对象最初是 const 吗?

问题描述这个代码是UB吗?IsthiscodeUB?structA{voidnonconst(){}};constA&a=A{};const_cast<A&>(a).nonconst();换句话说,(临时)对象最初是const吗?我浏览了标准,但找不到答案,因此希望能引用相关部分

发布:2022-10-16 标签:constantsc++undefined-behaviorlanguage-lawyertemporary-objects


为什么非 const 引用参数可以绑定到临时对象?

问题描述charf1();无效f2(char&);结构A{};一个f3();无效f4(A&);主函数(){f2(f1());//错误C2664.这正如预期的那样.f4(f3());//行!为什么???}错误C2664:'voidf4(char&)':无法从'char'转换参数1到

发布:2022-10-16 标签:constantsreferencec++object-lifetimetemporary-objects


我是否正确地说 const_cast 然后修改绑定到临时的 ref-to-const 可以吗?

问题描述我想检查一下我对此事的理解和结论.Iwouldliketocheckmyunderstandingandconclusionsonthismatter.在IRC上,有人问:OnIRC,itwasasked:const_cast是否可以接受绑定到临时对象的const引用?I

发布:2022-10-16 标签:constantsreferencec++const-casttemporary-objects


为什么可以将非常量引用参数绑定到临时对象?

问题描述charf1();voidf2(char&);结构体{};一个f3();voidf4(A&);intmain(){f2(f1());//错误C2664.这正如预期的那样.f4(f3());//行!为什么???}错误C2664:voidf4(char&)":无法从char"

发布:2022-10-16 标签:constantsreferencec++object-lifetimetemporary-objects


我说 const_cast 然后修改绑定到临时对象的 ref-to-const 是对的吗?

问题描述我想检查一下我对此事的理解和结论.Iwouldliketocheckmyunderstandingandconclusionsonthismatter.在IRC上,有人问:OnIRC,itwasasked:const_cast是否可以接受绑定到临时对象的const引用?I

发布:2022-10-16 标签:constantsreferencec++const-casttemporary-objects


在函数作用域(生命周期)之后,对临时对象的 const 引用被破坏

问题描述在问这个问题时,我学会了const引用到临时对象在C++中是有效的:Whileaskingthisquestion,IlearnedconstreferencetoatemporaryobjectisvalidinC++:intmain(){inta=21;intb=21

发布:2022-10-16 标签:referencec++scopeconst-referencetemporary-objects


std:ostringstream 和 -std=c++11 的内存错误?

问题描述编辑:感谢所有指出问题并在StackOverflow上讨论过的人.我自己投了最后一票.EDIT:Thankstoeveryonewhopointedouttheproblem,andthatitwasdiscussedonStackOverflow.Icastthelas

发布:2022-10-16 标签:memory-leaksc++c++11stringstreamtemporary-objects