草庐IT

move_function_imp

全部标签

c++ - 制作一个 const unique_ptr 然后尝试从它 std::move 给出相同的错误,就好像您试图访问复制构造函数一样

当我们尝试复制unique_ptr(例如,将一个唯一指针分配给另一个)时,我注意到错误是ErrorC2280std::unique_ptr#includeintmain(){std::unique_ptra=std::make_unique(2);std::unique_ptrb=a;}没关系,因为unique_ptr没有定义复制构造函数。您不会从唯一指针进行复制以在它们之间move(转移指针的所有权)。有趣的是(好吧,也许不是),这段代码抛出了同样的错误。现在我知道它是无效的(我将第一个unique_ptr声明为不可变对象(immutable对象)),但错误消息暗示它正在尝试调用复制

c++ - 模板实例化失败 : compiler choosing improper overload function

我对模板并不陌生,但我遇到了一个相当奇怪的问题,我需要将模板类型分离到它的组件中,以用于我正在处理的数据序列化程序。这很难解释,所以我已经证明了。这是我简化的示例问题,example.cpp。templatevoidfoo(T&arg){}templatevoidfoo(T&arg){}intmain(intargc,char*argv[]){foo(argc);return0;}我得到一个错误,然后是一个警告,这似乎表明它正在尝试实例化两个函数,但只有其中一个是合适的。$g++-Wall-Wexample.cppexample.cpp:2:43:error:‘T’isnotatemp

c++ - syntax::function_name 在 C++ 中是什么意思?

在名为::foo()的函数中,我不明白语法的用途。如果它是foo::count_all()那么我知道count_all是类或命名空间foo的函数。在::foo()的情况下,::引用的是什么? 最佳答案 ::运算符正在调用namespace或class。在您的情况下,它正在调用全局命名空间,它是不在命名空间中的所有内容。下面的例子说明了为什么namespace很重要。如果您只是调用foo(),您的调用将无法解析,因为有2个foo。您需要使用::foo()解决全局问题。namespaceHidden{intfoo();}intfoo()

c++ - 错误 : no matching member function for call to 'push_back'

为什么我在最后两行收到错误?目标是在集合中找到对象,并修改其内容。usingnamespacestd;structmystruct{intid;vectory;mystruct(constintid):id(id){}booloperatorsx;mystructx(1);x.y.push_back(1);x.y.push_back(2);sx.insert(x);//set::iteratori=sx.find(1);constmystruct*x1=&(*i);constmystructx2=*x1;couty)y)y.push_back(4);}好像迭代器返回的是常量对象,不让我

c++ - 复制初始化: why move or copy constructor was not called even if copy-elision is turned off?

我的问题不同,因为我可能“知道”复制省略。我正在学习复制初始化。但是,以下代码让我感到困惑,因为我已经使用-fno-elide-contructors-O0选项关闭了复制省略。#includeusingnamespacestd;classtest{public:test(inta_,intb_):a{a_},b{b_}{}test(consttest&other){cout我首先使用命令构建:g++-std=c++11-fno-elide-constructors-O0main.cpp-omain得到如下结果:**showelideconstructors**moveconstruct

c++ - 错误 : no matching function for call to . .. 在返回语句

我在openSUSELeap15上的Qt5.9.4上使用GCC7。我有以下类(class):classManSuppProps:publicQObject{Q_OBJECTpublic:explicitManSuppProps(QStringparentName);explicitManSuppProps(){}explicitManSuppProps(constManSuppProps&manSuppProps);explicitManSuppProps(ManSuppProps&manSuppProps);~ManSuppProps();private:QVector3Dm_sup

c++ - 如何调用对象的成员函数作为 std 算法的 unary_function?

我有一个看起来像这样的类。classA{public:voiddoSomething();}我有一组这些类。我想对数组中的每个项目调用doSomething()。使用算法header执行此操作的最简单方法是什么? 最佳答案 使用std::mem_fun_ref将成员函数包装为一元函数。#include#includestd::vectorthe_vector;...std::for_each(the_vector.begin(),the_vector.end(),std::mem_fun_ref(&A::doSomething));

c++ - 哪个更好 : Function overriding or passing a function pointer for event handling

因此,我正在为一个类编写代码,该类将进入一个供其他人使用的库。此类将拦截和处理传入的消息(细节并不重要,但它使用activemq-cpp库)。这个消费类的轮廓是classMessageConsumer{...public:voidrunConsumer();virtualvoidonMessage(constMessage*message);}其中runConsumer()建立连接并开始监听,并在收到消息时调用onMessage()。我的问题是:使用此代码的人将各自有自己的方式来处理不同的消息。我怎样才能保持MessageConsumer通用但提供这种灵active,同时保持代码简单?

c++ - 将 boost::bind 与 boost::function 一起使用:检索绑定(bind)变量类型

有什么方法可以检索有关哪些参数受boost::bind限制的信息,还是需要手动存储?即:在.h中classMyClass{voidfoo(inta);voidfoo2(doubleb);voidbar();voidexecute();int_myint;double_mydouble;}在.cpp中MyClass::bar(){vectormyVector;myVector.push_back(boost::bind(&MyClass::foo,this,MyClass::_myint);myVector.push_back(boost::bind(&MyClass::foo2,thi

C++ 如何在不使用 winapi 的情况下 move 文件并将它们从一个磁盘复制到不同的磁盘?

必须是纯c++,我知道system("copyc:\\test.txtd:\\test.txt");但我认为这是系统功能,而不是c++解决方案,否则我会出错吗? 最佳答案 std::fstream怎么样?打开一个用于阅读,另一个用于写入,然后使用std::copy让标准库处理复制。像这样:voidcopy_file(conststd::string&from,conststd::string&to){std::ifstreamis(from,ios::in|ios::binary);std::ofstreamos(to,ios::o