move-constructor

How to find move constructors in codebase using Clang AST tools?

旧街凉风 提交于 2019-12-25 07:38:31
问题 Following up a comment from this question: how can I find move constructors in C++ codebase using Clang AST tools? (find definitions / declarations only) 回答1: The Clang AST matcher now provides this functionality with the isMoveConstructor matcher. Here's an example program: #include <iostream> #include "clang/AST/AST.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/SourceLocation.h"

Neither copy nor move constructor called [duplicate]

允我心安 提交于 2019-12-24 21:15:31
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why copy constructor is not called in this case? What are copy elision and return value optimization? Can anybody explain to me why the following program yields output "cpy: 0" (at least when compiled with g++ 4.5.2): #include<iostream> struct A { bool cpy; A() : cpy (false) { } A (const A & a) : cpy (true) { } A (A && a) : cpy (true) { }; }; A returnA () { return A (); } int main() { A a ( returnA () ); std:

Why does this C++0x program generates unexpected output?

心已入冬 提交于 2019-12-24 01:08:31
问题 This program: test_header.hpp #include <boost/signal.hpp> #include <utility> class Sensor; class Recorder : public ::boost::signals::trackable { public: explicit Recorder(int id) : id_(id) {} // Cannot be copied Recorder(const Recorder &) = delete; Recorder &operator =(const Recorder &) = delete; // But can be moved so it can be stored in a vector. // There's a proposal for having a compiler generated default for this that would be // very convenient. Recorder(Recorder &&b) : id_(b.id_) { b

How to detect whether a class has a move constructor?

爱⌒轻易说出口 提交于 2019-12-23 20:53:06
问题 I'd like to detect (and use the result in std::enable_if ) whether a C++ class has a move constructor defined. The following program prints MOVE , so using std::is_move_constructible is not the way to do it: #include <stdio.h> #include <type_traits> class C { public: C() { puts("C()"); } C(int) { puts("C(int)"); } ~C() { puts("~C()"); } C(const C&) { puts("C(const C&)"); } // C(C&&) { puts("C(C&&)"); } C& operator=(const C&) { puts("C="); return *this; } }; int main(int argc, char** argv) {

Why does move constructor affect is_assignable?

荒凉一梦 提交于 2019-12-23 17:53:12
问题 Just came from is_assignable and std::unique_ptr. @Angew tells me that because std::unique_ptr<int, do_nothing> and std::unique_ptr<int> are different types, so static_assert(not std::is_assignable<std::unique_ptr<int>, std::unique_ptr<int, do_nothing>>::value, ""); . So, I tried: template<typename T, typename D> struct MoveAssignOnly_V2 { MoveAssignOnly_V2& operator=(MoveAssignOnly_V2&) = delete; MoveAssignOnly_V2& operator=(MoveAssignOnly_V2&&) noexcept {} }; int main() { static_assert(not

Move constructor with memcpy

南楼画角 提交于 2019-12-22 01:32:45
问题 I have a struct that i want to be non copyable, only movable, but as it contains a lot of POD, writing move constructor would be long and forgetting a variable would be hard to debug. Example: struct myStruct{ int a,b,c,d; double e,f,g,h; std::complex<double> value1,value2; std::unique_ptr<Calculator> calc; myStruct(){} myStruct(const myStruct &)=delete; myStruct(myStruct && other); }; What would be the problems with this kind of move constructor: myStruct::myStruct(myStruct && other){ std:

Why do we need to set rvalue reference to null in move constructor?

僤鯓⒐⒋嵵緔 提交于 2019-12-21 07:55:58
问题 //code from https://skillsmatter.com/skillscasts/2188-move-semanticsperfect-forwarding-and-rvalue-references class Widget { public: Widget(Widget&& rhs) : pds(rhs.pds) // take source’s value { rhs.pds = nullptr; // why?? } private: struct DataStructure; DataStructure *pds; }; I can't understand the reason for setting rhd.pds to nullptr . What will happen if we remove this line : rhs.pds = nullptr; 回答1: Some details of the class have been removed. In particular, the constructor dynamically

Use std::move in C++11 move constructor with uniform initialization syntax

喜你入骨 提交于 2019-12-19 09:57:08
问题 I have this simple class: struct Worker { Worker() : done{false} {} Worker(const Worker& rhs) : done{rhs.done}, qworker{} {} Worker(Worker &&rhs) : done{rhs.done} { qworker = std::move(rhs.qworker); } ... } this compile fine with gcc-4.7.2 but if I try to use this version I obtain an error struct Worker { Worker() : done{false} {} Worker(const Worker& rhs) : done{rhs.done}, qworker{} {} Worker(Worker &&rhs) : done{rhs.done} , qworker{std::move(rhs.qworker)} // <- ERROR { } ... } Why? In file

Why does the compiler require a copying constructor, need and have moving one and doesn't uses any of them?

不羁的心 提交于 2019-12-19 04:38:36
问题 I've already tried to ask this question but I wasn't clear enough. So here is one more try. And I am very sorry for my English ;) Let's see the code: #include <iostream> #include <memory> using namespace std; struct A { unique_ptr<int> ref; void printRef() { if (ref.get()) cout<<"i="<<*ref<<endl; else cout<<"i=NULL"<<endl; } A(const int i) : ref(new int(i)) { cout<<"Constructor with "; printRef(); } ~A() { cout<<"Destructor with"; printRef(); } }; int main() { A a[2] = { 0, 1 }; return 0; }

Implementing Move Constructor by Calling Move Assignment Operator

送分小仙女□ 提交于 2019-12-18 10:44:23
问题 The MSDN article, How to: Write a Move Constuctor, has the following recommendation. If you provide both a move constructor and a move assignment operator for your class, you can eliminate redundant code by writing the move constructor to call the move assignment operator. The following example shows a revised version of the move constructor that calls the move assignment operator: // Move constructor. MemoryBlock(MemoryBlock&& other) : _data(NULL) , _length(0) { *this = std::move(other); }