草庐IT

move_member

全部标签

c++ - 使用 bind2nd() : "member function already defined or declared" instead of "reference to reference" 的奇怪编译器错误

我最近花了很多时间来理解在这段代码中调用func()时的错误消息:intmain(){vector>v;doublesum=0;for_each(v.begin(),v.end(),bind2nd(ptr_fun(func),&sum));return0;}当func()像这样声明时,代码编译正常:voidfunc(vectorv,double*sum){}当我使用这个声明(为了提高效率)时,我得到了一个编译器错误:voidfunc(constvector&v,double*sum){}我期望看到的错误类似于reference-to-reference错误,因为binder2nd的op

c++ - 通过 destruct+move 构造的 move 分配安全吗?

这里有一个非常简单的方法来为大多数带有move构造函数的类定义move赋值:classFoo{public:Foo(Foo&&foo);//youstillhavetowritethisoneFoo&operator=(Foo&&foo){if(this!=&foo){//avoiddestructingtheonlycopythis->~Foo();//callyourowndestructornew(this)Foo(std::move(foo));//callmoveconstructorviaplacementnew}return*this;}//...};在标准C++11中,

c++ - move 构造函数不能默认

我是c++11的新手并编写了以下类,我希望它支持std::move:classX{public:X(intx):x_(x){}~X(){printf("X(%d)hasbereleased.\n",x_);}X(X&&)=default;X&operator=(X&&)=default;X(constX&)=delete;X&operator=(constX&)=delete;private:intx_;};但是,当我使用选项-std=c++0x编译它时,编译器会报错如下:queue_test.cc:12:error:‘X::X(X&&)’cannotbedefaultedqueue_

c++ - move 分配给自己的行为

这个问题在这里已经有了答案:Whatdoesthestandardlibraryguaranteeaboutselfmoveassignment?(2个答案)关闭8年前。示例代码:#includeintmain(){std::vectorw(20,123),x;w=std::move(w);std::coutg++4.8.3上的输出:0当然,标准规定move赋值运算符使操作数处于未指定状态。例如,如果代码是x=std::move(w);那么我们期望w.size()为零。但是,是否有特定的顺序或其他条款涵盖自行搬家的情况?size是否为0未说明或20,或其他东西,或未定义的行为?标准容器

c++ - 默认 move 构造函数和引用成员

来自N3337的[12.8][11]:Theimplicitly-definedcopy/moveconstructorforanon-unionclassXperformsamemberwisecopy/moveofitsbasesandmembers.[Note:brace-or-equal-initializersofnon-staticdatamembersareignored.Seealsotheexamplein12.6.2.—endnote]Theorderofinitializationisthesameastheorderofinitializationofbases

C++ 错误 : Member declaration not found

我是一个C++新手。今天遇到一个问题:在头文件中,我定义了一个类:templateclassPtr_to_const{private:Array_Data*ap;unsignedsub;public:...Ptr_to_const&operator=(constPtr_to_const&p);};在源文件中,我编程为:templatePtr_to_const&Ptr_to_const::operator=(constPtr_to_const&p){...return*this;}编译时,编译器总是说:'找不到成员声明'。为什么?我使用eclipseCDT+CygwinGCC非常感谢!

c++ - 使用不可复制(但可 move )键 move 分配 map 时出错

为什么这不起作用:#include#includestd::map,std::unique_ptr>foo();std::map,std::unique_ptr>barmap;intmain(){barmap=foo();return0;}虽然这样做:#include#includestd::map,std::unique_ptr>foo();std::map,std::unique_ptr>barmap;intmain(){std::map,std::unique_ptr>tmp(foo());usingstd::swap;swap(barmap,tmp);return0;}这与映射

c++ - std::move() 是否会使迭代器无效?

这个问题在这里已经有了答案:Doesmovingavectorinvalidateiterators?(4个答案)关闭5年前。考虑以下程序:structlist_wrapper{std::vectorm_list;};intmain(){std::vectormyList{1,1,2,3,5};conststd::vector::iteratoriter=myList.begin();list_wrapperwrappedList;wrappedList.m_list=std::move(myList);//CanIstilldereferenceiter?return0;}调用std

c++ - Clang claims that `member reference base type ' X' is not a structure or union`,但 X 是具有推导参数的结构模板

考虑以下代码:templatestructX{X(T){}voidfoo(){}};templatestructY{intobject=0;voidbar(){X(object).foo();}};Liveongcc.godbold.orgGCC8.2编译它,而Clang7吐出以下错误::13:18:error:memberreferencebasetype'X'isnotastructureorunionX(object).foo();~~~~~~~~~^~~~这对我来说像是一个错误。条件非常具体:如果任一结构不是模板,或者object不是成员变量,或者不涉及CTAD(类模板参数推导

c++ - "Member is private"虽然我不从外部访问它,但在使用尾随返回类型时

如何解决以下问题?我正在编写一些函数库,它定义了以下与这个问题相关的函数:call(f,arg):调用带有参数的函数。只是我在某些情况下需要的包装器。comp(f1,f2):返回两个函数的组合。返回表示两个函数组合的辅助仿函数。实现如下所示(简化版本仍能说明问题)://Callfwithoneargumenttemplateautocall(constFn&f,constArg&arg)->decltype(f(arg)){returnf(arg);}//HelperfunctorforthefunctionbelowtemplateclassCompFn{Fn1a;Fn2b;publ