move-semantics

Perfect forwarding with multiple passes over input arguments

我的梦境 提交于 2019-12-06 05:43:39
问题 Consider the following function accept that takes a "universal reference" of type T and forwards that to a parse<T>() function object with an overload for lvalues and one for rvalues: template<class T> void accept(T&& arg) { parse<T>()(std::forward<T>(arg), 0); // copy or move, depending on rvaluedness of arg } template<class T> class parse { // parse will modify a local copy or move of its input parameter void operator()(T const& arg, int n) const { /* optimized for lvalues */ } void

Replacing std::function from within itself (by move-assignment to *this?)

喜欢而已 提交于 2019-12-06 05:13:47
问题 Is it possible to replace one std::function from within itself with another std::function ? The following code does not compile: #include <iostream> #include <functional> int main() { std::function<void()> func = []() { std::cout << "a\n"; *this = std::move([]() { std::cout << "b\n"; }); }; func(); func(); func(); } Can it be modified to compile? The error message right now is: 'this' was not captured for this lambda function - which I completely understand. I don't know, however, how I could

move Constructor is not called

萝らか妹 提交于 2019-12-06 04:13:22
I am implementing a IntArray Class for learning C++. I must admit I haven't fully understood r and lvalues and move constructors, yet. I wanted to try it out to see if my code is working, but I do not know why {IntArray array = IntArray(5);} doesn't call my implemented move constructor. I thought this would be a case for it. #include "IntArray.h" IntArray::IntArray() :data(nullptr), count(0), capacity(0) {std::cout << "Default Constructor called" << std::endl;} IntArray::IntArray(int size) :data(new int[size]), count(size), capacity(size) {std::cout << "Constructor called with size: " << size

How to get if a type is truly move constructible

南笙酒味 提交于 2019-12-06 03:37:23
问题 Take for example this code: #include <type_traits> #include <iostream> struct Foo { Foo() = default; Foo(Foo&&) = delete; Foo(const Foo&) noexcept { std::cout << "copy!" << std::endl; }; }; struct Bar : Foo {}; static_assert(!std::is_move_constructible_v<Foo>, "Foo shouldn't be move constructible"); // This would error if uncommented //static_assert(!std::is_move_constructible_v<Bar>, "Bar shouldn't be move constructible"); int main() { Bar bar {}; Bar barTwo { std::move(bar) }; // prints

Is clang Xcode 4.4.1 buggy when -fno-elide-constructors is set?

我们两清 提交于 2019-12-06 03:33:29
I'm trying to educate myself on move constructors and move assignment so I can get my students started on this feature of C++11. I've seen (and explained elsewhere on this site) that compilers will optimize away the move (or ordinary) construction in most cases so you can't see it at work. With gcc 4.7.0, the -fno-elide-constructors option will turn that off and you can see move construction happening. But that same flag is supposed(?) to be clang option, and when I specify it as "Other C++ flags" in Xcode 4.4.1 with clang, c++11, and stdc++, apparently a returned value doesn't get copied or

Is std::move safe in an arguments list when the argument is forwarded, not move constructed?

纵然是瞬间 提交于 2019-12-06 02:46:35
问题 Trying to provide a solution to std::string_view and std::string in std::unordered_set, I'm playing around with replacing std::unordered_set<std::string> with std::unordered_map<std::string_view, std::unique_ptr<std::string>> (the value is std::unique_ptr<std::string> because the small string optimization would mean that the address of the string 's underlying data will not always be transferred as a result of std::move . My original test code, that seems to work, is (omitting headers): using

C++03 moving a vector into a class member through constructor (move semantics)

大兔子大兔子 提交于 2019-12-06 01:27:15
问题 I only have access to C++03 and I often want to move a vector into a function the way you can do it in C++11. The question how to do it not to confuse the user of the code too much. So my question is how did programmers do it before C++11. I know that vector can be "moved" using swap function. So here is what I have come up with: class Foo { public: Foo(std::vector<int>& vec) { using std::swap; swap(vec, m_vec); // "move" vec into member vector } private: std::vector<int> m_vec; }; // usage:

How do move semantics work with unique_ptr?

馋奶兔 提交于 2019-12-06 01:24:52
I was experimenting with using unique_ptr and wrote some simple code to check how it works with move semantics. #include <iostream> #include <vector> using namespace std; class X { public: X(){} ~X() { cout << "Destructor X" << endl; } void Print() { cout << "X" << endl; } }; int main() { unique_ptr<X> ptr(new X()); ptr->Print(); vector<unique_ptr<X>> v; v.push_back(move(ptr)); ptr->Print(); v.front()->Print(); return 0; } The output is as follows: X X X Destructor X My expectation was that the original unique_ptr ptr would be invalidated after the push_back. But the Print() method is called

std::vector<Foo> when some members of Foo are references

陌路散爱 提交于 2019-12-06 00:34:15
问题 I often prefer to use references than pointers whenever possible, it makes the syntax cleaner in my opinion. In this case, I have a class: class Foo { public: Foo(Bar & bar) : bar_(bar) {} private: Bar & bar_; }; operator=() is implicitely deleted by the compiler for such a class, since once a reference is set, it cannot be changed (I can technically define my own that doesn't change bar_, but this would not be the required behaviour, so I'd rather the compiler complain if I try to assign a

What does it mean “xvalue has identity”?

一笑奈何 提交于 2019-12-05 23:31:44
问题 C++11 introduced new value categories, one of them is xvalue . It is explained by Stroustrup as something like ( im category): "it is a value, which has identity, but can be moved from". Another source, cppreference explains: a glvalue is an expression whose evaluation determines the identity of an object, bit-field, or function; And xvalue is a glvalue , so this is statement is true for xvalue too. Now, I thought that if an xvalue has identity, then I can check if two xvalue s refer to the