草庐IT

move_member

全部标签

c++ - 为什么 std::move 返回的对象不立即销毁

我测试了以下代码:#includeusingnamespacestd;classfoo{public:foo(){cout输出是:foo()statement"move(f);"done.~foo()但是,我预计:foo()~foo()statement"move(f);"done.根据函数move的源码:templateconstexprtypenamestd::remove_reference::type&&move(_Tp&&__t)noexcept{returnstatic_cast::type&&>(__t);}返回的对象是正确的值,为什么不立即销毁呢?-----------

c++ - 在 C++ 中, "_MOVE_H"有什么特别之处?

我有这样一个C++文件#ifndef_MOVE_H#define_MOVE_HclassMove{intx,y;public:Move(intinitX=0,intinitY=0):x(initX),y(initY){}intgetX(){returnx;}voidsetX(intnewX){x=newX;}intgetY(){returny;}voidsetY(intnewY){y=newY;}};#endif令我惊讶的是,#ifndef和#endif之间的所有代码都被编译器忽略了(我发誓我没有定义_MOVE_H其他任何地方),我有各种关于缺少定义的错误。我在想我做错了什么,但是当我

c++ - 为什么我们不能有 move 的迭代器?

C++11引入了一种“move”算法,它的行为类似于“复制”算法,除了它......move数据而不是复制数据。我想知道为什么委员会没有更新复制算法以使用forward代替(或者可能除此之外)。vector提供了T&的迭代器constvector提供constT&的迭代器vector&&不能提供T&&的迭代器是有原因的吗?这将允许通过使用vector的构造函数将元素从列表move到vector...这是个坏主意吗? 最佳答案 我们已经有了。使用std::make_move_iterator创建move迭代器。

c++ - std::forward vs std::move 同时将左值绑定(bind)到右值引用

这里move和forward有区别吗:voidtest(int&&val){val=4;}voidmain(){intnb;test(std::forward(nb));test(std::move(nb));std::cin.ignore();} 最佳答案 在您的具体情况下,不,没有任何区别。详细答案:在幕后,std::move(t)做static_cast::type&&>(t),其中T是t的类型(参见§20.2.3/6)。在您的情况下,它解析为static_cast(nb).forward有点棘手,因为它是为在模板中使用而量身

c++ - 在 Mac 上安装 xgboost 失败 - ar : no archive members specified

我正在尝试在Mac上安装xgboost。我按照github上的说明进行操作,但是当我运行make-j4时出现错误:c++-std=c++0x-Wall-O3-msse2-Wno-unknown-pragmas-funroll-loops-Iinclude-Idmlc-core/include-Irabit/include-fPIC-DDISABLE_OPENMP-oxgboostbuild/cli_main.obuild/learner.obuild/logging.obuild/c_api/c_api.obuild/c_api/c_api_error.obuild/common/co

c++ - 类型特征 : Check if reference member variable is static or not

我想检查一个类的成员变量是否是静态的。使用std::is_member_pointer适用于除引用成员之外的所有类型。#includestructA{intfoo;};structB:A{};structC{staticintfoo;};structD:C{};structE{int&foo;};structF{staticint&foo;};static_assert(std::is_member_pointer::value,"No");static_assert(std::is_member_pointer::value,"No");static_assert(!std::is_

c++ - operator= 的模糊重载使用 move 分配和按值复制分配传递

如果我定义一个复制赋值运算符,它使用类thing的按值传递调用复制构造函数:thing&operator=(thingx){和同一个类的move赋值运算符:thing&operator=(thing&&x){尝试调用move赋值导致gcc出错:error:ambiguousoverloadfor‘operator=’(operandtypesare‘thing’and‘std::remove_reference::type{akathing}’)但是,如果复制分配改为使用引用传递:thing&operator=(thing&x){编译没问题,两个操作符都可以调用。为什么是这样?完整的C

c++ - “Default member initializer needed within definition of enclosing class outside of member functions” - 我的代码格式不正确吗?

structfoo{structbar{~bar(){}//noerrorw/othisline};bar*data=nullptr;//noerrorw/othislinefoo()noexcept=default;//noerrorw/othisline};是的,我知道,还有一个题目完全相同,但有点不同的问题(涉及noexceptoperator和没有嵌套类型)。那里建议的解决方案(将foo的构造函数替换为foo()noexcept{})改变了语义,这里没有必要:这里我们有一个更好的答案(因此问题不是重复的)。编译器:AppleLLVM版本9.0.0(clang-900.0.37)

c++ - Qt C++ : Moving a file, 源路径和目标路径在不同的驱动器上

我想在C++中将一个文件从一个文件夹(比如在驱动器C上)移动到另一个文件夹(比如在驱动器D上)。如果该文件已经存在于目标文件夹中,它应该覆盖它。如何使用C++std库或Qt实现它?我找到了“重命名”方法,但我不确定如果路径位于不同的驱动器上,它是否会起作用。另外,什么是平台依赖? 最佳答案 只需使用QFile::rename().对于大多数目的,它应该做大致正确的事情。我认为C++标准库没有文件系统间重命名调用(如果我错了,请在评论中纠正我!),std::rename只能在单个文件系统内移动。然而,通常这里唯一(相关的)原子文件操作

C++ 模板 : conditionally enabled member function

我正在创建一个非常小的C++项目,我想创建一个简单的vector类来满足我自己的需要。std::vector模板类不会做。当vector类由char组成时s(即vector),我希望它能够与std::string进行比较.经过一番折腾之后,我编写了既能编译又能执行我想要的操作的代码。见下文:#include#include#includetemplateclassWorkingSimpleVector{public:constElementType*elements_;size_tcount_;//...templateinlinetypenamestd::enable_if::val