草庐IT

c++ - 优化器是否将临时非 POD 类型移出循环?

给定以下代码:while(is_running){std::vectorbuffers;//fillbuffers//usebuffers}现代编译器是否执行以下转换?std::vectorbuffers;while(is_running){//fillbuffers//usebuffersbuffers.clear();} 最佳答案 确定性知道的唯一方法是测试,但是如果看到优化器执行此优化,我会感到相当惊讶。甚至要开始执行此优化,编译器必须1)充分了解所涉及函数的内部结构以“实现”(例如)operatornew和operatord

c++ - 临时实例的常量正确性

这里是“作用域锁”习语的例子,它有一个常见的错误:没有创建局部变量,所以锁没有生效。这段代码在VC++2010和ComeauC++在线编译都完美无缺:classMutex{public:voidlock(){}};classScopedLock{public:ScopedLock():m_pm(0){}ScopedLock(Mutex&m):m_pm(&m){m_pm->lock();}private:Mutex*m_pm;private:ScopedLock&operator=(constScopedLock&);ScopedLock(constScopedLock&);};clas

c++ - 避免在 std::map/std::unordered_map 中使用 std::string 键查找临时对象

这个问题在这里已经有了答案:Avoidingkeyconstructionforstd::map::find()(4个答案)关闭8年前。考虑以下代码:std::mapm1;autoi=m1.find("foo");constchar*key=...autoj=m1.find(key);这将为每次map查找创建一个临时的std::string对象。避免它的规范方法是什么?

c++ - 返回具有已删除移动/复制构造函数的临时类型

考虑以下程序:#includeusingnamespacestd;structS{S()=default;S(constS&other)=delete;S(S&&other)=delete;inti;};SnakedBrace(){return{};//noSconstructedhere?}StypedBrace(){returnS{};}intmain(){//produceanobservableeffect.cout示例session:$g++-Wall-std=c++14-ono-copy-ctorno-copy-ctor.cppno-copy-ctor.cpp:Infunc

c++ - C++ 临时对象的生命周期是在什么时候创建的? : expression extended by binding it to a local const reference?

我不清楚是否可以通过将临时对象绑定(bind)到?:表达式中的常量引用来延长临时对象的生命周期:classFoo{...};Foo*someLValue=...;constFoo&=someLValue?*someLValue:Foo();通过调用默认构造函数Foo()创建的临时对象的生命周期是否通过将其绑定(bind)到本地constref来延长,即使绑定(bind)是有条件的?还是因为Foo()的临时值会在?:表达式的末尾被销毁,所以这会创建一个悬空引用? 最佳答案 在此代码中,条件运算符的第二个和第三个操作数具有不同的值类别(

c++ - 不要临时调用 QString::operator[]()

我运行clazy在我的代码上并获得有关此类代码的警告:QCharvalue()const{if(hide_content_)return'\0';elsereturntext()[0];}text()有这样的签名QStringtext()const;警告是:warning:Don'tcallQString::operator[]()ontemporary[-Wclazy-detaching-temporary]returntext()[0];^但这是什么意思呢?临时QString对象是否有可能被销毁在调用operator[]之前? 最佳答案

c++ - 在没有返回值优化的情况下将两个对象相加会创建多少个临时对象?

在阅读ScottMeyers的“更有效的C++”一书的第20和22项之后,我决定问这个问题。假设您编写了一个类来表示有理数:classRational{public:Rational(intnumerator=0,intdenominator=1);intnumerator()const;intdenominator()const;Rational&operator+=(constRational&rhs);//Doesnotcreateanytemporaryobjects...};现在假设您决定使用operator+=实现operator+:constRationaloperato

c++ - 绑定(bind)到引用时临时对象生命周期扩展异常的基本原理是什么?

在C++11标准的12.2中:Thetemporarytowhichthereferenceisboundorthetemporarythatisthecompleteobjectofasubobjecttowhichthereferenceisboundpersistsforthelifetimeofthereferenceexcept:Atemporaryboundtoareferencememberinaconstructor’sctor-initializer(12.6.2)persistsuntiltheconstructorexits.Atemporaryboundtoar

c++ - 从临时对象返回左值引用

当*this是右值时,是否允许返回对*this的左值引用?#include#includeusingnamespacestd;classA{public:A&f(){return*this;}stringval()const{return"works";}};intmain(){coutf()返回的值在某些时候是否会成为悬空引用?如果这是一个右值,那么调用f()会延长调用者的生命周期吗? 最佳答案 *this从来都不是右值,但在这种情况下,它是(对)临时值的(引用)。临时对象在定义它们的语句完成之前是有效的对象,即直到代码到达终止;

c++ - 如何以允许传递临时对象的方式将 std::istream 传递给函数?

我正在尝试创建一个构造函数来从给定的任何istream加载资源。我似乎无法找出将istream参数传递给构造函数的最佳方法。Loader::Loader(istreamstream);由于对象切片,这个显然很糟糕,所以别无选择。Loader::Loader(istream&stream);这就是我现在使用的,看起来还不错。但是它有一个重要的问题——你不能给它一个临时的,因为临时的不能绑定(bind)到非常量引用!例如,以下将不起作用:Container():mLoader(ifstream("path/file.txt",ios::binary){}这是一个相当大的限制,因为我现在被迫