草庐IT

C++, 'if' 表达式中的变量声明

这是怎么回事?if(inta=Func1()){//Works.}if((inta=Func1())){//Failstocompile.}if((inta=Func1())&&(intb=Func2()))){//Dostuffwithaandb.//ThisiswhatI'dreallyliketobeabletodo.}2003标准中的第6.4.3节解释了在选择语句条件中声明的变量如何具有扩展到条件控制的子语句末尾的范围。但我看不出它在哪里说了不能在声明两边加上括号,也没有说每个条件只能有一个声明。即使在条件中只需要一个声明的情况下,这种限制也很烦人。考虑一下。boola=fal

c++ - "warning: operation of ... may be undefined"用于三元运算——不是 if/else block

这个问题在这里已经有了答案:Undefinedbehaviorandsequencepoints(5个答案)关闭6年前。这是我的代码:intmain(){staticinttest=0;constintanotherInt=1;test=anotherInt>test?test++:0;if(anotherInt>test)test++;elsetest=0;return0;}这是我构建它时产生的警告:../main.cpp:15:40:warning:operationon‘test’maybeundefined[-Wsequence-point]test=anotherInt>te

c++ - 将 C++11 move 语义应用于绑定(bind)函数

我有一些现有的C++98代码,它们使用boost::function和boost:bind进行异步回调。一些相关的简化代码片段包括:typedefboost::functionWriteHandler;structWriteOperation{WriteOperation(constboost::shared_ptr&device,conststd::string&data,constWriteHandler&handler):m_Device(device),m_Data(data),m_Handler(handler){}private:boost::shared_ptrm_Dev

c++ - 在没有 move 构造函数的情况下返回不可复制对象的解决方法

在我的API中,我有一个返回std::istringstream的函数.std::istringstreamclass是不可复制的,但支持move,因此在符合标准的编译器上返回本地没有问题std::istringstream.但是,在gcc4.9上,有nosupportmovestd::istringstream.有没有我可以使用的解决方法std::istringstream无需从用户的角度更改API?建议的解决方法here,使用unique_ptr将改变API的语义。 最佳答案 如果你不能movestd::istringstrea

c++ - vector 增长时如何强制执行 move 语义?

我有一个std::vector对象的某个类A。该类非常重要,并且定义了复制构造函数和move构造函数。std::vectormyvec;如果我用A对象填充vector(使用例如myvec.push_back(a)),vector的大小会增加,使用复制构造函数A(constA&)实例化vector中元素的新拷贝。我能否以某种方式强制开始使用类A的move构造函数? 最佳答案 您需要使用noexcept通知C++(特别是std::vector)您的move构造函数和析构函数不会抛出异常。然后move构造函数将在vector增长时被调用。

c++ - 双参数和 move 语义

假设我有以下foo函数:Widgetfoo(Widgetlhs,Widgetrhs){returnlhs.bar(rhs);}然后我想在两边使用相同的参数:Widgetbaz(Widgetw){returnfoo(w,w);}碰巧Widget很大,我想避免复制太多。假设bar就位,我可以执行以下操作:Widgetbaz(Widgetw){returnfoo(std::move(w),w);}这只会制作一份拷贝。但我担心这是不正确的代码,因为参数传递顺序在C++中未指定,我可能会给出一个移出的参数。我改为执行以下操作:Widgetbaz(Widgetw){Widgetw_bis(w);r

c++ - constexpr if 和 static_assert

P0292R1constexprif一直included,在C++17的轨道上。它似乎很有用(并且可以替代SFINAE的使用),但是关于static_assert的评论是错误的,不需要诊断在false分支中吓到我了:Disarmingstatic_assertdeclarationsinthenon-takenbranchofaconstexprifisnotproposed.voidf(){ifconstexpr(false)static_assert(false);//ill-formed}templatevoidg(){ifconstexpr(false)static_asser

c++ - std::remove_if 和 erase 不从 std::vector 中移除元素

我正在练习leetcodeeasy问题。我想使用lambda从vector中删除_if(这是第一次,太棒了)。我得到一个指向new_end的负指针。#include#include#include#include//std::greaterusingnamespacestd;intmain(){vectora={2,7,11,15};inttarget=9;autonew_end=std::remove_if(a.begin(),a.end(),[&a,target](constintx){returnstd::count(a.begin(),a.end(),x)>target;});

c++ - 在构造函数中初始化成员时,是否应该在成员上使用 std::move?

我一直在努力完全理解移动语义,但我有一个问题,因为不同的示例显示不同的东西。假设我们有一个Foo类,它有一个字符串成员str_。要定义移动构造函数,我应该这样定义它吗:Foo(Foo&&foo):str_(foo.str_){}或者这个:Foo(Foo&&foo):str_(std::move(foo.str_)){}此外,我是否需要将要移动的对象的成员设置为空值?如果不构造另一个字符串,我将如何做到这一点,从根本上抵消了首先使用移动构造函数节省的费用? 最佳答案 你应该使用第二种方法。您不必对您移动的string做任何事情,因为这

c++ - C++17 中的 "If constexpr"在非模板函数中不起作用

我尝试使用C++17标准。我尝试使用C++17ifconstexpr的功能之一。我有一个问题......请看下面的代码。这编译没有错误。在下面的代码中,我尝试使用ifconstexpr来检查它是否是一个指针。#include#includetemplatevoidprint(Tvalue){ifconstexpr(std::is_pointer_v)std::cout但是当我重写上面的代码时,如下所示,其中ifconstexpr在main函数中:#include#includeintmain(){autovalue=100;ifconstexpr(std::is_pointer_v)s