草庐IT

c++ - 标准 C++11 是否保证传递给函数的临时对象会在函数结束后被销毁?

众所周知,标准C++11保证传递给函数的临时对象将在函数调用之前创建:DoesstandardC++11guaranteethattemporaryobjectpassedtoafunctionwillhavebeencreatedbeforefunctioncall?但是,标准C++11是否保证传递给函数的临时对象会在函数结束后(而不是之前)被销毁?C++编程语言标准工作草案2016-07-12:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf§12.2Temporaryobjects§12.2/5Th

c++ - 将临时结构作为模板参数传递

我正在创建一个vector类,并试图找出为不同大小的vector重用最大数量代码的方法。这是一个基本示例:templateclassVector{public:union{Tv[D];struct{/*Tx;*Ty;*Tz;*Tw;*/};};Vector(){for(unsignedinti=0;i我希望成员变量可以公开访问。例如:Vectorvec;printf("X:%.2f,Y:%.2f\n",vec.x,vec.y);我想做的是类似这样的事情:templateclassVector2:publicVector{};templateclassVector3:publicVect

c++ - 为什么临时成员函数不绑定(bind)到正确的类型?

假设我们有以下基类和派生类:#include#includeclassCar{public:voidDrive(){std::cout..还有以下模板函数:templatevoidFunction(void(T::*m1)(void),void(V::*m2)(void)){std::cout为什么使用GCC编译:intmain(intargc,char**argv){void(Porsche::*ptr)(void)=&Porsche::Drive;Function(ptr,ptr);return0;}...但不是这个?intmain(intargc,char**argv){void

c++ - 这个 C++ 临时绑定(bind)到引用成员应该是非法的吗?

我的问题(将在这之后提出,抱歉介绍太长,问题在粗体中)最初是受HerbSuttersExceptionalC++我们在哪里找到这样的东西:...intmain(){GenericTableAlgorithma("Customer",MyWorker());a.Process();}withclassGenericTableAlgorithm{public:GenericTableAlgorithm(conststring&table,GTAClient&worker);boolProcess();private:structGenericTableAlgorithmImpl*pimpl

C++ - 临时变量及其生命周期

这个问题可以被认为是以下问题的后续问题:C++temporaryvariablelifetime.Qt容器支持stream-like初始化语法。现在,当我编写以下代码时,我的QVector在赋值后立即销毁,引用变为悬空。constQVector&v=QVector()对应operator实现方式如下:inlineQVector&operator据我所知,10.4.10TemporaryObjects声明临时对象的生命周期被延长以匹配相应的生命周期const引用它。但是,在这种情况下,临时对象QVector()较早销毁。我想这可能是由于最后一个操作返回了QVector&而发生的。并且不应

c++ - 交换引用的临时元组

我正在编写一个自定义迭代器,它在取消引用时返回一个引用元组。由于元组本身是短暂的,我认为我无法从operator*()返回引用。我认为我的迭代器在语义上是有意义的,因为它具有引用语义,即使operator*返回一个值也是如此。问题是,当我尝试调用std::swap时(或者更确切地说,当std::sort调用时),如下所示,我收到错误,因为交换需要左值。有解决此问题的简单方法吗?#includeclasstest{public:test():v1(10),v2(10){}classiterator{public:iterator(std::vector&_v1,std::vector&_

c++ - 如何防止构造函数被临时调用

我有一个类,其构造函数接受一些vector并存储它们。structX{X(std::vectorconst&ints,std::vectorconst&floats,std::vectorconst&strings):ints_{ints},floats_{floats},strings_{strings}{};std::vectorints_;std::vectorfloats_;std::vectorstrings_;};我想将成员变量变成引用,因为在生产代码中,传递给构造函数的值是左值,其生命周期比类X的对象长。但是,单元测试通常使用临时变量构造Xes,如下所示:Xx{{42},

c++ - 临时变量的生命范围

#include#includevoidfun(constchar*c){printf("-->%s\n",c);}std::stringget(){std::stringstr="HelloWorld";returnstr;}intmain(){constchar*cc=get().c_str();//ccisnotvalidatthispoint.Asitispointingto//temporarystringinternalbuffer,andthetemporarystring//hasalreadybeendestroyedatthispoint.fun(cc);//But

c++ - 将临时对象传递给采用指针的函数

我尝试了以下代码:#include#includeusingnamespacestd;stringf1(strings){returns="f1called";}voidf2(string*s){cout但是这段代码无法编译。我的想法是:f1按值返回,因此它创建了临时地址,我正在获取地址并将其传递给f2。现在请解释我哪里想错了? 最佳答案 一元&采用左值(或函数名)。函数f1()不返回左值,它返回一个右值(对于返回某些东西的函数,除非它返回一个引用,否则它的返回值是一个右值),所以一元&不能应用于它。

c++ - 当我在 C++ 中将临时 int 分配给 const 引用时会发生什么?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Doesaconstreferenceprolongthelifeofatemporary?假设我有一个函数f:intf(intx){returnx;}constint&a=f(1);我知道f(1)只是一个临时的,我会在这条语句之后被销毁,但是将引用设为const会使f(1)的生命周期更长吗?如果是,f(1)将存储在哪里?这是否意味着x在超出范围时也没有被销毁?f(1)和x有什么区别?