c++ - How do I use the `Function Call Operator` to load `rvalue` types to my object? -


i have class this:

template<typename t> class myclass {     public:         // ...               t && operator()(uint64_t i, uint64_t j);  // want add member function this.               t &  operator()(uint64_t i, uint64_t j);         const t &  operator()(uint64_t i, uint64_t j) const;         // ... }; 

while revising code, realized stored object of type t being copied every time tries set object (i,j) position. use move semantics , avoid unnecessary copying if possible? possible solve adding third operator in code above?

my aim access myclass instance in code:

myclass<element> myclass; // ... element element;                        // 'element' movable. // ... myclass(2, 3) = std::move(element);     // 'element' no longer needed, can moved. 

how can this?

first of can't overload based on return type. however, not problem since don't need overloads of subscript operator.

in order avoid unnecessary copies , since class element objects movable, use std::swap instead of direct assignment.

std::swap(myclass(2, 3), element); 

since c++11 std::swap avoids unnecessary copy because uses move semantics.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -