草庐IT

c++ - 是否有将引用返回到临时文件的 C++ 警告?

这个案例有一个错误:constint&foo(){constintx=0;returnx;}甚至constint&foo(){conststd::pairx={0,0};returnx.first;}但不是这个:constint&foo(){conststd::arrayx={0};returnx[0];}并且(不那么令人惊讶)不是这个:constint&foo(){conststd::vectorx={0};returnx[0];}特别是在std::vector在这种情况下,我知道这个警告会非常棘手,因为对于编译器来说constint&并不明显由std::vector::operat

c++ - 为什么规定临时对象必须有不同的地址?

我感兴趣的情况是constint&n1=123;constint&n2=123;我知道这好像字面量123是用于初始化临时int的参数,而const只是无聊的编译时间检查,但我想知道为什么在这种情况下需要不同的临时文件,而不是n1和n2都具有相同的临时文件。我知道theruleexists但不知道为什么存在这个规则。 最佳答案 constint&n1=123;constint&n2=123;Iwanttoknowthereasonwhydistincttemporariesareneededinthiscase.因为C++委员会可能不

c++ - decltype-specifier 表示的类型是什么类型,其表达式是类类型的临时对象的成员?

假设我们有以下声明:structS{inta;};以下简单类型说明符表示的类型是什么?是int还是int&&?decltype(S{}.a)(这个问题旨在解决C++17,但也感谢解决其他版本标准的答案。) 最佳答案 它是int。每[dcl.type.simple]/4:Foranexpressione,thetypedenotedbydecltype(e)isdefinedasfollows:[...]otherwise,ifeisanunparenthesizedid-expressionoranunparenthesizedcl

c++ - BST前序遍历并将树内容写入临时数组

我正在尝试将二叉搜索树的内容写入临时数组以便在main中使用。但是我不确定该怎么做......我试过这样的事情:voidBook::preorder(TreeNode*ptr,Person&temp[],intx){if(ptr!=NULL){temp[x].name=ptr->item.name;x++;preorder(ptr->left,temp,x);preorder(ptr->right,temp,x);}}而且,它给出了以下错误:declarationof'temp'aasarrayofreferencesnomatchfor'operator[]'in'((Book*)t

c++ - 将临时对象附加到对 const 的引用时出错

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:typedefandcontainersofconstpointers为什么代码会发出错误?intmain(){//testcodetypedefint&Ref_to_int;constRef_to_intref=10;}错误是:error:invalidinitializationofnon-constreferenceoftype‘int&’fromatemporaryoftype‘int’我阅读了prolongingthelifetimeoftemporaries上的帖子这表示临时对象可以绑定(bind

c++ - 在 C++ 中将未命名的临时变量括起来

考虑以下代码:structFoo{};structBar{explicitBar(constFoo&){}};intmain(){Foofoo;Barbar(foo);//Okay.Bar(foo);//Willnotcompile.(Bar(foo));//Okay.Unnamedtemporaryrequiresparenthesis.}为什么需要临时版本周围的括号?他们解决了什么歧义?我的直觉是:我认为编译器将Bar(foo)视为函数的声明,但我不确定为什么会这样,因为foo(实例)不是类型。因此,括号强制将上述内容视为表达式,而不是前向声明。 最佳答

c++ - 在 C++11 中哪些临时对象不能用 `someType()` 初始化?

我有一些对类型和初始值进行操作的宏。我需要将初始值vIni转换为vType(vIni总是可转换为vType,有时它有同一类型)。vIni也可能为空,在这种情况下,vType应该未初始化或默认初始化。结果传递给模板化函数。简而言之,templatevoidfoo(constT&o);foo(vType(vIni));foo(vType());必须编译。我已经发现foo(unsignedint())或foo(int*())无法编译,但可以使用typedef解决。还有哪些其他情况(除了带有空格和指针的内置类型)会失败? 最佳答案 简短(稍

c++ - 三元运算符并通过常量引用延长临时对象的生命周期

看到之后alocalreference-to-constmayprolongthelifeofatemporary,我遇到了有条件地将本地常量引用绑定(bind)到函数参数或函数调用的临时结果的需要,即:classGizmo{//RuleofFivemembersimplemented};GizmoFrobnicate(constGizmo&arg);voidProcessGizmo(constGizmo&arg,boolfrobnicate){constFoo&local=frobnicate?Frobnicate(arg):arg;//Performsomeworkonlocal}

C++临时类实例化不明确

让我们将过程形成为类。只有构造函数调用会产生一些副作用。调用后无需在内存中处理类实例。以下代码实例化该类:structA{A(int){}};intmain(){A(0);//right.Passconsttoctorintx=0;A(x);//bad.CompilerinterpretlikeAx;(A(x));//right.NewtemporaryobjectpassedtobracketsA((int)x);//right.Passtemporaryinttoctorreturn0;}(另见OnlineIDE)为什么是A(x);解释为变量x声明而不是临时A对象实例化?

c++ - 将临时变量写入 Json : I get\u0000

我面临一个奇怪的问题:当我尝试在for循环中添加一个Json变量时,它没有正确写入输出文件,而它在循环外运行良好(rapidJsonv0.11)。编辑:循环不是问题,但即使只有括号也会出现错误这是我的代码示例:rapidjson::Documentoutput;output.SetObject();rapidjson::Document::AllocatorType&allocator=output.GetAllocator();{std::strings1("test");output.AddMember("test_field",s1.c_str(),allocator);}std