move-semantics

Allocator propagation policies in your new modern C++ containers

依然范特西╮ 提交于 2019-12-08 10:52:53
问题 What is the reason for having these traits in a container (https://en.cppreference.com/w/cpp/memory/allocator_traits) propagate_on_container_copy_assignment Alloc::propagate_on_container_copy_assignment if present, otherwise std::false_type propagate_on_container_move_assignment Alloc::propagate_on_container_move_assignment if present, otherwise std::false_type propagate_on_container_swap Alloc::propagate_on_container_swap if present, otherwise std::false_type is_always_equal(since C++17)

Get an rvalue when calling a getter method on an rvalue object

邮差的信 提交于 2019-12-08 02:04:52
问题 Suppose, I have the following code. There's a copy constructor in B which calls a method which copies the resources of a. Now I also have a move constructor. In this case, a should not be copied but just "steal" the resources from an existing a. Therefore, I also implemented an init taking an rvalue. But of course, when I try to call it with parameter b.a, this is an lvalue... Is there a way to call this method? class A{ A(const A&& a){ // 'steal' resources from a } void init(A& a){ // init

returning a string from a function

巧了我就是萌 提交于 2019-12-07 15:52:06
问题 I wanted to write a function that'll be cross platform (win32 & linux), and return a string representation of the datetime [hh:mm:ss dd-mm-yyyy]. Knowing that I just want to use the returned string as a temporary in a stream fashion as below: std::cout << DateTime() << std::endl; I considered writing a function with the following prototype const char* DateTime(); If you return a character array, you must delete it once you're done. But I just want a temporary, I don't want to have to worry

C++11: call by value, move semantics and inheritance

馋奶兔 提交于 2019-12-07 10:49:15
问题 Let's say I have a class which I plan to directly expose as an instantiatable class to the programmer: class Base { public: Base(std::string text) : m_text(std::move(text)) {} private: std::string m_text; }; So far so good. No need for a rvalue-constructor here. Now, at some point in the future, I decide to extend Base: class Derived : public Base { public: Derived(const std::string &text) : Base(text) {} }; This bugs me: I can't take the string by value in Derived because that's what Base is

Why can't I pass an rvalue std::stringstream by value to a function?

隐身守侯 提交于 2019-12-07 09:46:14
问题 Why does this code not compile? #include <sstream> void f(std::stringstream) { } int main() { f(std::stringstream{}); } I get this error: error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’ f(std::stringstream{}); ^ If I replace std::stringstream with another type that's noncopyable it works fine. Shouldn't this use stringstream 's move constructor? 回答1: The missing move constructor for stringstream is a known missing

What kinds of types does qsort not work for in C++?

守給你的承諾、 提交于 2019-12-07 05:24:58
问题 std::sort swaps elements by using std::swap , which in turn uses the copy constructor and assignment operators, guaranteeing that you get correct semantics when exchanging the values. qsort swaps elements by simply swapping the elements' underlying bits, ignoring any semantics associated with the types you are swapping. Even though qsort is ignorant of the semantics of the types you are sorting, it still works remarkably well with non-trivial types. If I'm not mistaken, it will work with all

Lvalue reference constructor is called instead of rvalue reference constructor

左心房为你撑大大i 提交于 2019-12-07 04:21:50
问题 There is this code: #include <iostream> class F { public: F() = default; F(F&&) { std::cout << "F(F&&)" << std::endl; } F(F&) { std::cout << "F(F&)" << std::endl; } }; class G { F f_; public: G(F&& f) : f_(f) { std::cout << "G()" << std::endl; } }; int main(){ G g = F(); return 0; } The output is: F(F&) G() Why F(F&) constructor is called instead of F(F&&) constructor in constructor of class G ? The parameter for constructor of class G is F&& f which is rvalue reference but constructor for

Is there any case where a return of a RValue Reference (&&) is useful?

安稳与你 提交于 2019-12-07 04:01:05
问题 Is there a reason when a function should return a RValue Reference ? A technique, or trick, or an idiom or pattern? MyClass&& func( ... ); I am aware of the danger of returning references in general, but sometimes we do it anyway, don't we ( T& T::operator=(T) is just one idiomatic example). But how about T&& func(...) ? Is there any general place where we would gain from doing that? Probably different when one writes library or API code, compared to just client code? 回答1: There are a few

Static arrays VS. dynamic arrays in C++11

霸气de小男生 提交于 2019-12-07 02:03:01
问题 I know that it's a very old debate that has already been discussed many times all over the world. But I'm currently having troubles deciding which method I should use rather than another between static and dynamic arrays in a particular case. Actually, I woudn't have used C++11, I would have used static arrays. But I'm now confused since there could be equivalent benefits with both. First solution: template<size_t N> class Foo { private: int array[N]; public: // Some functions } Second

Why is `T(const T&&)` called a move constructor?

二次信任 提交于 2019-12-07 01:52:32
问题 [C++11: 12.8/3]: A non-template constructor for class X is a move constructor if its first parameter is of typeX&& , const X&& , volatile X&& , or const volatile X&& , and either there are no other parameters or else all other parameters have default arguments (8.3.6). [..] Why is a constructor that takes a const rvalue reference called a "move constructor" by the standard? Surely it's self-evident that this prohibits meaningful move semantics in all but the most fringey cases? "According to