C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types...

37
C ++ : to copy, ref, or move? Matthias Kretz Frankfurt Institute for Advanced Studies Institute for Computer Science Goethe University Frankfurt March 26, 2014 HGS-HIRe Helmholtz Graduate School for Hadron and Ion Research

Transcript of C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types...

Page 1: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

C++: to copy, ref, or move?

Matthias Kretz

Frankfurt Institute for Advanced StudiesInstitute for Computer Science

Goethe University Frankfurt

March 26, 2014

HGS-HIReHelmholtz Graduate School for Hadron and Ion Research

Page 2: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

Function Parameters

Guidelines

AddonsPerfect ForwardingConst-Ref to Temporary

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 2FIAS Frankfurt Institutefor Advanced Studies

Page 3: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

void f(std::vector<int>);

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 3FIAS Frankfurt Institutefor Advanced Studies

Page 4: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

void f(std::vector<int> &);void f(std::vector<int> *);

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 4FIAS Frankfurt Institutefor Advanced Studies

Page 5: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

void f(const std::vector<int> &);void f(const std::vector<int> *);

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 5FIAS Frankfurt Institutefor Advanced Studies

Page 6: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

void f(std::vector<int> &&);

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 6FIAS Frankfurt Institutefor Advanced Studies

Page 7: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

template <typename T>void f(T &&);

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 7FIAS Frankfurt Institutefor Advanced Studies

Page 8: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

template <typename T>void f(T &&x) {

g(std::forward<T>(x));}

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 8FIAS Frankfurt Institutefor Advanced Studies

Page 9: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

c© by INABA Tomoaki CC BY-SA @flickr

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 9FIAS Frankfurt Institutefor Advanced Studies

Page 10: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

types that fit into a registerpass by value

I copy is very cheapI reference requires the value to be in memoryI reference requires a register to point to the object

int, float, double, void *, . . .

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 10FIAS Frankfurt Institutefor Advanced Studies

Page 11: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

I struct A { int x, y, z; };fits in two registers.

I struct B { float x, y, z; };fits in two registers (%xmm0[0], %xmm0[1], %xmm1).

(depends on the ABI, I’m referring to the Linux x86_64 ABI)

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 11FIAS Frankfurt Institutefor Advanced Studies

Page 12: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

local copies increase optimization

I The compiler knows exactly where the variables aremodified

I Non-local variables could get modified by any function call(note that most function calling conventions guarantee thecontents of several registers to stay unmodified)

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 12FIAS Frankfurt Institutefor Advanced Studies

Page 13: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

complex typespass by const reference

I especially types with dynamically allocated dataI copy of std::string or std::vector<T>:

I mallocI std::copy

calls T::T(const T &)

I reference requires:I the complete object to reside at one memory locationI a pointer to this locationI pass this pointer via register/stack

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 13FIAS Frankfurt Institutefor Advanced Studies

Page 14: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

the alternative to copy and reference is

move

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 14FIAS Frankfurt Institutefor Advanced Studies

Page 15: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

On rvalues and lvalues

C++ has two categories of values:lvalue an object with a name (a variable)

originally: left hand side of an assignmentrvalue a temporary object (there’s no name to access it)

originally: right hand side of an assignmentI xvalue: (eXpiring value) a value at the end of

its lifetimeI prvalue: (pure rvalue) literals & function return

values that are not a reference (“any rvaluethat is not an xvalue”)

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 15FIAS Frankfurt Institutefor Advanced Studies

Page 16: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

On rvalues and lvalues

int x = 1; // ’1’ is a prvalueint y = x; // ’x’ is an lvalueint z = std::move(x); // ’x’ is casted to

// an xvalue

int & // lvalue referenceint && // rvalue reference

auto && // "universal reference" (Scott Meyers)T &&

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 16FIAS Frankfurt Institutefor Advanced Studies

Page 17: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

void f(const A &);void f(A &&);

...{f(A{});

}

Calls ?

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 17FIAS Frankfurt Institutefor Advanced Studies

Page 18: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

void f(const A &);void f(A &&);

...{f(A{});

}

Calls f(A &&)

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 17FIAS Frankfurt Institutefor Advanced Studies

Page 19: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

movable typesif applicable, move instead of copy/reference

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 18FIAS Frankfurt Institutefor Advanced Studies

Page 20: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

What is a movable type?

Or: what does a move constructor do?class A {Data *data;

public:A() : data(new Data) {}~A() { delete data; }A(const A &from) : data(new Data(*from.data)) {}A(A &&from) : data(from.data) {from.data = nullptr;

}};

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 19FIAS Frankfurt Institutefor Advanced Studies

Page 21: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

What is a movable type?

Or: what does a move constructor do?class A {

Data *data;public:

A() : data(new Data) {}~A() { delete data; }A(const A &from) : data(new Data(*from.data)) {}A(A &&from) : data(from.data) {from.data = nullptr;

}};

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 19FIAS Frankfurt Institutefor Advanced Studies

Page 22: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

You don’t need to write all this, thanks to unique_ptr.This is equivalent:class A {std::unique_ptr<Data> data;

public:A() : data(new Data) {}

};

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 20FIAS Frankfurt Institutefor Advanced Studies

Page 23: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

In C++11 string and all containers (except array) aremoveable types.class A {

string name;public:A(const string &n): name(n)

{}};...A x{"Hello World"};

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 21FIAS Frankfurt Institutefor Advanced Studies

Page 24: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

In C++11 string and all containers (except array) aremoveable types.class A {

string name;public:A(const string &n): name(n)

{}};...A x{"Hello World"};

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 21FIAS Frankfurt Institutefor Advanced Studies

Page 25: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

In C++11 string and all containers (except array) aremoveable types.class A {

const string &name;public:A(const string &n): name(n)

{}};...A x{"Hello World"};

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 21FIAS Frankfurt Institutefor Advanced Studies

Page 26: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

In C++11 string and all containers (except array) aremoveable types.class A {

string name;public:A(string n): name(n)

{}};...A x{"Hello World"};

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 21FIAS Frankfurt Institutefor Advanced Studies

Page 27: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

In C++11 string and all containers (except array) aremoveable types.class A {

string name;public:A(string n): name(std::move(n))

{}};...A x{"Hello World"};

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 21FIAS Frankfurt Institutefor Advanced Studies

Page 28: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

Function Parameters

Guidelines

AddonsPerfect ForwardingConst-Ref to Temporary

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 22FIAS Frankfurt Institutefor Advanced Studies

Page 29: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

Perfect Forwarding

template <typename T> void f(T x);template <typename T> void f(T & x);template <typename T> void f(T &&x);

void h() {int x = 1;const int y = 1;f(x); // (1) lvaluef(y); // (2) const lvaluef(1); // (3) rvalue

}

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 23FIAS Frankfurt Institutefor Advanced Studies

Page 30: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

Perfect Forwarding

template <typename T> void f(T x);template <typename T> void f(T & x);template <typename T> void f(T &&x);

void h() {int x = 1;const int y = 1;f(x); // (1) lvaluef(y); // (2) const lvaluef(1); // (3) rvalue

}

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 23FIAS Frankfurt Institutefor Advanced Studies

Page 31: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

Perfect Forwarding

template <typename T> void f(T x);template <typename T> void f(T & x);template <typename T> void f(T &&x);

void h() {int x = 1;const int y = 1;f(x); // (1) lvaluef(y); // (2) const lvaluef(1); // (3) rvalue

}

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 23FIAS Frankfurt Institutefor Advanced Studies

Page 32: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

std::forward

template <typename T> void f(T &&x) {g(x);

}

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 24FIAS Frankfurt Institutefor Advanced Studies

Page 33: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

std::forward

template <typename T> void f(T &&x) {g(std::move(x));

}

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 24FIAS Frankfurt Institutefor Advanced Studies

Page 34: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

std::forward

template <typename T> void f(T &&x) {g(std::forward<T>(x));

}

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 24FIAS Frankfurt Institutefor Advanced Studies

Page 35: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

void f(const A &);

...{f(A{});

}

Does this work?And why?

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 25FIAS Frankfurt Institutefor Advanced Studies

Page 36: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

void f(const A &);

...{f(A{});

}

works: temporary exists until the end of the statement(also: const-ref to temporary extends lifetime)

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 25FIAS Frankfurt Institutefor Advanced Studies

Page 37: C++: to copy, ref, or move?+__to_copy,_r… · Function Parameters Guidelines Addons complex types pass by const reference I especially types with dynamically allocated data I copy

Function Parameters Guidelines Addons

void f(A &);

...{f(A{});

}

error: invalid initialization of non-const reference of type ’A &’ from anrvalue of type ’A’

Matthias Kretz (compeng.uni-frankfurt.de) March 26, 2014 26FIAS Frankfurt Institutefor Advanced Studies