草庐IT

move_if_noexcept

全部标签

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

c++ - 将函数标识符作为右值引用传递并对其应用 std::move()

考虑以下片段#include#includeusingcallback=std::function;doublesum(doublea,doubleb){returna+b;}intmain(intargc,char*argv[]){//Shouldn'tthisleavesum()inaninvalidstate?autoc=std::move(sum);std::cout我正在将sum转换为右值引用,将其存储在c中,并在没有明显错误行为的情况下调用这两个函数。这是为什么?std::move不应该让sum处于无效状态吗? 最佳答案

在if语句中使用字符串

我需要一种方法让用户输入在if陈述:print("Hello,World!")name=input("Whatisyourname?")hobby=input("Cool,so"+name+"whatdoyoudoyouforfun?Youcansaysomethinglikeplay,work,learn,etc.")play='play'work='work'learn='learn'ifhobby=playprint('awesome')elifhobby=workprint('mustbebusy')elifhobby=learnprint('ha,metoo')看答案这if,elif

c++ - 特殊成员函数是 noexcept 还是 throw()?

C++11规范明确指出隐式生成的特殊函数(即默认构造函数、析构函数、复制/移动构造函数和复制/移动赋值运算符)具有异常规范。但该规范似乎仅根据现已弃用的动态异常规范(即“throw(T1,T2,T3)”)编写。15.4/14中的示例支持这一点:structA{A();A(constA&)throw();A(A&&)throw();~A()throw(X);};structB{B()throw();B(constB&)throw();B(B&&)throw(Y);~B()throw(Y);};structD:publicA,publicB{//ImplicitdeclarationofD

c++ - move 语义、标准集合和构造时间地址

当然,我想知道一些神奇的解决方法,但我对重组持开放态度。所以我有一个类DeviceDependent,具有以下构造函数DeviceDependent(Device&device);它存储对设备的引用。该设备可以更改状态,这将需要更改所有DeviceDependent依赖于该设备的实例。(你猜对了,这是我尝试驾驭directX野兽的微不足道的尝试)为了处理这个,我有函数DeviceDependent::createDeviceResources(),DeviceDependent::onDeviceLost().我计划注册每个DeviceDependent实例到DeviceDepende

c++ - 对于 CLang 中的 enable_if 错误(错误 11723)是否有更好的解决方法?

理想情况下,我们可以使用enable_if做类似的事情:#includenamespacedetail{enumclassenabler_t{DUMMY};}templateusingenable_if_u=typenamestd::enable_if::type;templateusingdisable_if_u=typenamestd::enable_if::type;template::value>...>inta(){return0;}template::value>...>doublea(){return0.0;}intmain(){autox=a();}恕我直言,这是最好的

c++ - noexcept 和模板可能存在的 g++ 错误

当我将模板与noexcept说明符结合使用时,我收到有关不匹配的noexcept说明的错误。它编译了我用过的各种版本的clang和fails在所有版本的gcc中。structY{voidh();};templatestructX{voidf()noexcept(noexcept(std::declval().h()));};templatevoidX::f()noexcept(noexcept(std::declval().h())){}intmain(){}错误:g++-std=c++1y-O2-Wall-pthreadmain.cpp&&./a.outmain.cpp:15:56:

具有复制省略或 move 语义的 C++11 元组

我写了一个如下的函数:templatestd::tuple,T,T>f(){std::vectorp(1000);returnstd::make_tuple(std::move(p),10,10);}由于返回类型非常复杂,是否保证在c++11下编译器在构造结果时将应用复制省略或move语义,或者我必须明确地说出类似std::move(std::make_tuple(std::move(p),10,10))? 最佳答案 AFAIK复制省略始终是可选的。标准只是明确表示允许编译器进行此类优化,因为它改变了可观察到的行为,它并不强制执行。

c++ - 可以从 istream_iterator 制作 move_iterator 吗?

考虑以下代码:typedefistream_iteratorchar_itr;char_itreos;stringll("sometexthere");istringstreamline_in(ll);char_itrstart(line_in);move_iteratormstart(start);//!!!move_iteratormeos(eos);vectorvc(mstart,meos);上面的代码由于行(!!!)而无法编译:errorC2440:'return':cannotconvertfrom'constchar'to'char&&'但是,如果您分别用start和eos