move-semantics

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

给你一囗甜甜゛ 提交于 2019-12-04 10:28:44
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 capture func 's this -pointer. I guess, it is not even a std::function inside the lambda, yet?! How

Does a default virtual destructor prevent compiler-generated move operations?

坚强是说给别人听的谎言 提交于 2019-12-04 09:54:34
问题 Inspired by the post Why does destructor disable generation of implicit move methods? , I was wondering if the same is true for the default virtual destructor, e.g. class WidgetBase // Base class of all widgets { public: virtual ~WidgetBase() = default; // ... }; As the class is intended to be a base class of a widget hierarchy I have to define its destructor virtual to avoid memory leaks and undefined behavior when working with base class pointers. On the other hand I don't want to prevent

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

百般思念 提交于 2019-12-04 09:18:58
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 namespace std::literals; int main(int argc, char **argv) { std::unordered_map<std::string_view, std:

What is use of the ref-qualifier `const &&`?

。_饼干妹妹 提交于 2019-12-04 08:03:55
问题 I've been digging around ref-qualifiers a bit, following on a previous question. Given the code sample below; #include <iostream> #include <string> #include <utility> struct A { std::string abc = "abc"; std::string& get() & { std::cout << "get() &" << std::endl; return abc; } std::string get() && { std::cout << "get() &&" << std::endl; return std::move(abc); } std::string const& get() const & { std::cout << "get() const &" << std::endl; return abc; } std::string get() const && { std::cout <<

How to get if a type is truly move constructible

和自甴很熟 提交于 2019-12-04 07:40:02
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 "copy!" } Because Bar is derived from Foo, it doesn't have a move constructor. It is still constructible

What does it mean “xvalue has identity”?

依然范特西╮ 提交于 2019-12-04 05:02:07
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 same object, so I take the address of an xvalue . As it turned out, it is not allowed: int main() { int

Does it make sense to reuse destructor logic by using std::swap in a move assignment operator?

非 Y 不嫁゛ 提交于 2019-12-04 04:55:16
问题 Consider the following: class Example : boost::noncopyable { HANDLE hExample; public: Example() { hExample = InitializeHandle(); } ~Example() { if (hExample == INVALID_HANDLE_VALUE) { return; } FreeHandle(hExample); } Example(Example && other) : hExample(other.hExample) { other.hExample = INVALID_HANDLE_VALUE; } Example& operator=(Example &&other) { std::swap(hExample, other.hExample); //? return *this; } }; My thinking here is that the destructor will be running on "other" shortly, and as

move semantics std::move how use it

非 Y 不嫁゛ 提交于 2019-12-04 04:43:12
问题 #include <type_traits> template<class T> typename std::remove_reference<T>::type&& move(T&& v) { return v; } void main() { int a; move(a); } Why doesn't this code compile? error C2440: 'return' : impossible to convert 'int' in 'int &&' 回答1: This is straight out of the C++0x draft standard (§20.2.3/6): template <class T> typename remove_reference<T>::type&& move(T&& t) noexcept; Returns : static_cast<typename remove_reference<T>::type&&>(t) . Consequently, if you change your move

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

那年仲夏 提交于 2019-12-04 04:31:11
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: std::vector<int> v(100, 1337); Foo foo(v); // v.empty() == true The problem with this approach is that

Why does resize() cause a copy, rather than a move, of a vector's content when capacity is exceeded? [duplicate]

故事扮演 提交于 2019-12-04 03:43:09
This question already has an answer here: How to enforce move semantics when a vector grows? 3 answers Given class X below (special member functions other than the one explicitly defined are not relevant for this experiment): struct X { X() { } X(int) { } X(X const&) { std::cout << "X(X const&)" << std::endl; } X(X&&) { std::cout << "X(X&&)" << std::endl; } }; The following program creates a vector of objects of type X and resizes it so that its capacity is exceeded and reallocation is forced: #include <iostream> #include <vector> int main() { std::vector<X> v(5); v.resize(v.capacity() + 1); }