copy-elision

Will any compiler actually ever elide these copies?

一曲冷凌霜 提交于 2019-12-10 13:37:19
问题 Given struct Range{ Range(double from, double to) : from(from), to(to) {} double from; double to; // if it matters to the compiler, we can add more fields here to make copying expensive }; struct Box{ Box(Range x, Range y) : x(x), y(y) {} Range x; Range y; }; someone said that in Box box(Range(0.0,1.0),Range(0.0,2.0)) , the compiler can avoid copying Range objects altogether by constructing them inside box to begin with. Does any compiler actually do this? My own attempts haven't succeeded.

std::vector initialization move/copy constructor of the element

大城市里の小女人 提交于 2019-12-10 11:06:48
问题 I have this piece of code: #include <iostream> #include <vector> using namespace std; class Foo{ public: Foo() noexcept {cout << "ctor" << endl;} Foo(const Foo&) noexcept {cout << "copy ctor" << endl;} Foo(Foo&&) noexcept {cout << "move ctor" << endl;} Foo& operator=(Foo&&) noexcept {cout << "move assn" << endl; return *this;} Foo& operator=(const Foo&) noexcept {cout << "copy assn" << endl; return *this;} ~Foo() noexcept {cout << "dtor" << endl;} }; int main() { Foo foo; vector<Foo> v; v

Passing by Value and copy elision optimization

房东的猫 提交于 2019-12-09 17:23:26
问题 I came upon the article http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/ Author's Advice: Don’t copy your function arguments. Instead, pass them by value and let the compiler do the copying. However, I don't quite get what benefits are gained in the two example presented in the article: //Don't T& T::operator=(T const& x) // x is a reference to the source { T tmp(x); // copy construction of tmp does the hard work swap(*this, tmp); // trade our resources for tmp's return *this; //

std::vector initialization move/copy constructor of the element

一世执手 提交于 2019-12-06 16:05:26
I have this piece of code: #include <iostream> #include <vector> using namespace std; class Foo{ public: Foo() noexcept {cout << "ctor" << endl;} Foo(const Foo&) noexcept {cout << "copy ctor" << endl;} Foo(Foo&&) noexcept {cout << "move ctor" << endl;} Foo& operator=(Foo&&) noexcept {cout << "move assn" << endl; return *this;} Foo& operator=(const Foo&) noexcept {cout << "copy assn" << endl; return *this;} ~Foo() noexcept {cout << "dtor" << endl;} }; int main() { Foo foo; vector<Foo> v; v.push_back(std::move(foo)); // comment the above 2 lines and replace by // vector<Foo> v{std::move(foo)}; }

Is copy/move elision allowed to make a program using deleted functions well-formed?

空扰寡人 提交于 2019-12-06 04:18:54
问题 Consider the following code: #include <iostream> struct Thing { Thing(void) {std::cout << __PRETTY_FUNCTION__ << std::endl;} Thing(Thing const &) = delete; Thing(Thing &&) = delete; Thing & operator =(Thing const &) = delete; Thing & operator =(Thing &&) = delete; }; int main() { Thing thing{Thing{}}; } I expect Thing thing{Thing{}}; statement to mean construction of temporary object of Thing class using default constructor and construction of thing object of Thing class using move

C++11 constructor argument: std::move and value or std::forward and rvalue reference

社会主义新天地 提交于 2019-12-05 04:00:39
问题 Which of the below two should be preferred and why? struct X { Y data_; explicit X(Y&& data): data_(std::forward<Y>(data)) {} }; vs struct X { Y data_; explicit X(Y data): data_(std::move(data)) {} }; 回答1: The two variants differ in functionality. The following statements work for the second one–but not for the first one: Y y; X x(y); If you are looking for the same functionality, the two variants should look as follows: struct X { Y data_; explicit X(const Y& data) : data_(data) { } explicit

Is copy/move elision allowed to make a program using deleted functions well-formed?

 ̄綄美尐妖づ 提交于 2019-12-04 10:29:49
Consider the following code: #include <iostream> struct Thing { Thing(void) {std::cout << __PRETTY_FUNCTION__ << std::endl;} Thing(Thing const &) = delete; Thing(Thing &&) = delete; Thing & operator =(Thing const &) = delete; Thing & operator =(Thing &&) = delete; }; int main() { Thing thing{Thing{}}; } I expect Thing thing{Thing{}}; statement to mean construction of temporary object of Thing class using default constructor and construction of thing object of Thing class using move constructor with just created temporary object as an argument. And I expect that this program to be considered

What is clang's 'range-loop-analysis' diagnostic about?

℡╲_俬逩灬. 提交于 2019-12-04 07:33:41
Background: Consider the following example : #include <iostream> #include <vector> int main() { std::vector<bool> vectorBool{false, true}; for(const auto &element : vectorBool) std::cout << std::boolalpha << element << ' '; return 0; } It emits the warning: test.cpp:6:21: warning: loop variable 'element' is always a copy because the range of type 'std::vector<bool>' does not return a reference [-Wrange-loop-analysis] for(const auto &element : vectorBool) std::cout << std::boolalpha << element << ' '; ^ test.cpp:6:9: note: use non-reference type 'std::_Bit_reference' for(const auto &element :

Passing by Value and copy elision optimization

ⅰ亾dé卋堺 提交于 2019-12-04 04:50:51
I came upon the article http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/ Author's Advice: Don’t copy your function arguments. Instead, pass them by value and let the compiler do the copying. However, I don't quite get what benefits are gained in the two example presented in the article: //Don't T& T::operator=(T const& x) // x is a reference to the source { T tmp(x); // copy construction of tmp does the hard work swap(*this, tmp); // trade our resources for tmp's return *this; // our (old) resources get destroyed with tmp } vs // DO T& operator=(T x) // x is a copy of the source;

Copy elision for pass-by-value arguments

守給你的承諾、 提交于 2019-12-04 00:00:52
问题 Given struct Range{ Range(double from, double to) : from(from), to(to) {} double from; double to; }; struct Box{ Box(Range x, Range y) : x(x), y(y) {} Range x; Range y; }; suppose we run Box box(Range(0.0,1.0),Range(0.0,2.0)) . Could a modern compiler with optimizations enabled avoid copying Range objects altogether during this construction? (i.e. construct the Range objects inside box to begin with?) 回答1: There are actually two copies being performed on each Range object passed to the