move-semantics

initializer_list immutable nature leads to excessive copying

一笑奈何 提交于 2019-11-30 23:02:45
问题 Why does the access to std::initializer_list not allow us to change its content? It's a big disadvantage of std::initializer_list when using it for its main purpose (to initialize a container), since it's use leads to excessive copy-construction/copy-assignment, instead of move-construction/move-assignment. #include <initializer_list> #include <iostream> #include <vector> #include <cstdlib> struct A { A() = default; A(A const &) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(A &&) { std

Is a data member of a temporary object an xvalue in C++11?

别来无恙 提交于 2019-11-30 22:37:16
问题 #include <vector> using namespace std; struct A { vector<int> coll; }; void f(const vector<int>&){} void f(vector<int>&&){} int main() { f(A().coll); // Is "A().coll" an xvalue? } Does C++11 guarantee f(A().coll) will call void f(vector<int>&&) ? 回答1: Yes. C++14 standard, §5.2.5/4.2, given E1.E2 : If E2 is a non-static data member and the type of E1 is “ cq1 vq1 X ”, and the type of E2 is “ cq2 vq2 T ”, the expression designates the named member of the object designated by the first

C++11: Do move semantics get involved for pass by value?

天大地大妈咪最大 提交于 2019-11-30 19:31:22
I've got an API that looks like this: void WriteDefaultFileOutput(std::wostream &str, std::wstring target) { //Some code that modifies target before printing it and such... } I'm wondering if it'd be sensible to enable move semantics by doing this: void WriteDefaultFileOutput(std::wostream &str, std::wstring&& target) { //As above } void WriteDefaultFileOutput(std::wostream &str, std::wstring const& target) { std::wstring tmp(target); WriteDefaultFileOutput(str, std::move(tmp)); } or is this just boilerplate that the compiler should be able to figure out anyway? "Pass by value" can mean either

Can a move constructor be implicit?

。_饼干妹妹 提交于 2019-11-30 17:50:49
Consider the following class: class A { public: std::string field_a; std::string field_b; } Now consider the following copy construction: A a1(a2); The copy construction will adequately copy A despite the lack of of an explicit copy constructor because the copy constructors for std::string will be called by the compiler generated implicit copy constructor. What I wish to know is, is the same true for move construction? EDIT : Testing here shows that: A a2(std::move(a1)); Will actually result in a copy construction, unless the specific move constructor: A( A && other ) : a(std::move(other.a)) {

Why does std::forward discard constexpr-ness?

守給你的承諾、 提交于 2019-11-30 17:45:28
Being not declared constexpr , std::forward will discard constexpr-ness for any function it forwards arguments to. Why is std::forward not declared constexpr itself so it can preserve constexpr-ness? Example: (tested with g++ snapshot-2011-02-19) #include <utility> template <typename T> constexpr int f(T x) { return -13;} template <typename T> constexpr int g(T&& x) { return f(std::forward<T>(x));} int main() { constexpr int j = f(3.5f); // next line does not compile: // error: ‘constexpr int g(T&&) [with T = float]’ is not a constexpr function constexpr int j2 = g(3.5f); } Note: technically,

When is the move constructor called in the `std::move()` function?

走远了吗. 提交于 2019-11-30 17:39:32
The function std::move() is defined as template<typename T> typename std::remove_reference<T>::type&& move(T && t) { return static_cast<typename std::remove_reference<T>::type&&>( t ); } There are four places where I can imagine the move constructor to be called: When the parameter is passed. When the cast is performed. When the result is returned. Not in the std::move() function itself but possibly at the place where the returned reference ultimately arrives. I would bet for number 4, but I'm not 100% sure, so please explain your answer. There is no move construction going on. std::move()

Does an exception use move semantics when thrown in C++11?

浪子不回头ぞ 提交于 2019-11-30 17:18:32
http://www.drdobbs.com/cpp/practical-c-error-handling-in-hybrid-env/197003350?pgno=4 In this article Herb Sutter explains that throwing an exception requires a copy of the exception as it's created as a temporary and therefore uses an std::auto_ptr to get round the copy overhead. In light of move semantics being made available in C++11 is this still necessary? I have just checked, and the Standard allows omitting the copy or move of an object specified by the operand of a throw expression into the exception object omitting the copy or move of the exception object into the catch clause variable

How can a unique_ptr be returned by value without std::move? [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-30 15:11:25
问题 This question already has answers here : Returning unique_ptr from functions (5 answers) Closed 5 years ago . std::unique_ptr<int> ptr() { std::unique_ptr<int> p(new int(3)); return p; // Why doesn't this require explicit move using std::move? } // Why didn't the data pointed to by 'p' is not destroyed here though p is not moved? int main() { std::unique_ptr<int> a = ptr(); // Why doesn't this require std::move? std::cout << *a; // Prints 3. } In the above code, the function ptr() returns a

How do I efficiently build a vector and an index of that vector while processing a data stream?

痞子三分冷 提交于 2019-11-30 14:00:46
I have a struct Foo : struct Foo { v: String, // Other data not important for the question } I want to handle a data stream and save the result into Vec<Foo> and also create an index for this Vec<Foo> on the field Foo::v . I want to use a HashMap<&str, usize> for the index, where the keys will be &Foo::v and the value is the position in the Vec<Foo> , but I'm open to other suggestions. I want to do the data stream handling as fast as possible, which requires not doing obvious things twice. For example, I want to: allocate a String only once per one data stream reading not search the index

Which std types are guaranteed to be empty/null after being used as arg in move constructor

你。 提交于 2019-11-30 13:21:42
I know shared_ptr , unique_ptr , weak_ptr are guaranteed to be empty after used as RVR argument in the constructor of the same type, but I wonder does standard specifies this for some other std:: types beside the ones I mentioned. Please note that I know that elements after move are left in valid but unspecified state, I am here interested for which types state is specified. A rule of thumb As a rule of thumb: The move-only types or types with shared reference semantics leave their moved-from object in an empty state. All other types leave unspecified values. Move-only types Move-only types