move-semantics

std::move() as performance bottleneck?

只谈情不闲聊 提交于 2019-12-10 17:25:16
问题 I have a custom ringbuffer implementation which uses a normal array allocated via new [] , and then uses std::move to move elements into the array. Here is the implementation of my push() method: void push(value_type&& value) { _content[_end] = std::move(value); // 9.2% of execution is spend here increment(); // 0.6% here } The objects I'm moving into the array are basically just a pointer and a std::unique_ptr : struct Task { Task() {} Function function; Batch *batch; }; And Function looks

Getting value from a collection without using the Clone trait

为君一笑 提交于 2019-12-10 16:13:13
问题 Is it possible to get a value from a collection and apply a method to it which accepts only self and not &self ? Minimal Working Example What I would like to write is something akin to: use std::collections::HashMap; fn get<B>(key: i32, h: HashMap<i32, Vec<(i32, B)>>) -> i32 where B: Into<i32> { let v: &Vec<(i32, B)> = h.get(&key).unwrap(); let val: &B = v.first().unwrap().1; // Do something to be able to call into // I only need the value as read-only // Does B have to implement the Clone

Move construction from const reference

断了今生、忘了曾经 提交于 2019-12-10 13:56:40
问题 I have the following situation where I need to move construct t2 from t1. Unfortunately it is not possible to do that (constness violation I suppose) What is the right approach to handle that transparently from the caller of foo ? (ie. without requiring a passing by value and an explicit std::move) struct T { T() = default; ~T() = default; T(T&&) = default; }; T foo(const T& t) { T t3; if (predicate) return t3; else return std::move(t); } int main() { T t1; T t2 = foo(t1); return 0; } 回答1:

How to enable move semantics when adding custom objects to a vector?

人盡茶涼 提交于 2019-12-10 13:48:12
问题 Below code passes objects that contain big vectors into a vector. I want this to be performant. Do I need to cast test to rvalue in the call to push_back ? Do I need to tell compiler how to move instances of struct Test ? Or does this all go automatically? int main() { struct Test { std::vector<size_t> vals; double sum; }; std::vector<Test> vecOfTest; vecOfTest.reserve(100000); for (size_t i = 0; i < 100000; i++) { Test test{}; test.vals.reserve(i); for (size_t j = 0; j < i; j++) { test.vals

Autogenerated move constructors causing illegal behavior

ε祈祈猫儿з 提交于 2019-12-10 13:33:53
问题 I asked a question about move constructors for which I haven't accepted an answer yet because I'm feeling more confused about certain aspects of the question even as I'm starting to get a grip on others. In particular, I've found a surprising case in which both g++ and clang++ generate incorrect move-constructors. Question summary g++ and clang++ apparently violate the rule that move-constructors are not generated when destructors are explicitly defined; why? Is this a bug, or am I

What's the most efficient way to reuse an iterator in Rust?

半城伤御伤魂 提交于 2019-12-10 13:17:32
问题 I'd like to reuse an iterator I made, so as to avoid paying to recreate it from scratch. But iterators don't seem to be clone able and collect moves the iterator so I can't reuse it. Here's more or less the equivalent of what I'm trying to do. let my_iter = my_string.unwrap_or("A").chars().flat_map(|c|c.to_uppercase()).map(|c| Tag::from(c).unwrap() ); let my_struct = { one: my_iter.collect(), two: my_iter.map(|c|{(c,Vec::new())}).collect(), three: my_iter.filter_map(|c|if c.predicate(){Some(c

Can a stack have an exception safe method for returning and removing the top element with move semantics?

家住魔仙堡 提交于 2019-12-10 12:53:56
问题 In an answer to a question about std::stack::pop() I claimed that the reason pop does not return the value is for exception safety reason (what happens if the copy constructor throws). @Konrad commented that now with move semantics this is no longer relevant. Is this true? AFAIK, move constructors can throw, but perhaps with noexcept it can still be achieved. For bonus points what thread safety guarantees can this operation supply? 回答1: Of course, not every type is move-enabled and C++0x even

Numpy array pass-by-value

╄→尐↘猪︶ㄣ 提交于 2019-12-10 11:54:22
问题 I have a numpy array that is changed by a function. After calling the function I want to proceed with the initial value of the array (value before calling the modifying function) # Init of the array array = np.array([1, 2, 3]) # Function that modifies array func(array) # Print the init value [1,2,3] print(array) Is there a way to pass the array by value or am I obligated to make a deep copy? 回答1: As I mentioned, np.ndarray objects are mutable data structures. This means that any variables

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

How can I reuse a box that I have moved the value out of?

梦想的初衷 提交于 2019-12-10 02:34:51
问题 I have some non-copyable type and a function that consumes and (maybe) produces it: type Foo = Vec<u8>; fn quux(_: Foo) -> Option<Foo> { Some(Vec::new()) } Now consider a type that is somehow conceptually very similar to Box : struct NotBox<T> { contents: T } We can write a function that temporarily moves out contents of the NotBox and puts something back in before returning it: fn bar(mut notbox: NotBox<Foo>) -> Option<NotBox<Foo>> { let foo = notbox.contents; // now `notbox` is "empty"