Questions tagged [stdmove]
117 questions
0
votes
0 answers
Moving my threaded timer class into a vector and starting in same loop has an odd effect
I have a timer class that I have written. Any single timer seems to work perfectly. I was trying to add a load of them to a vector and start a load of them, but because it has a thread member I needed to write a move/copy constructor. I decided to…

code_fodder
- 15,263
- 17
- 90
- 167
0
votes
2 answers
Pass by value/reference/rvalue with a std::move(str) arg
I have the following code:
//void func(const std::string &&i){
//void func(const std::string &i){
void func(const std::string i){
std::string val{i};
}
int main()
{
std::string d = "asdf";
func(std::move(d));
std::cout << d <<…

24n8
- 1,898
- 1
- 12
- 25
0
votes
0 answers
is it safe to move std::shared_ptr by another thread
In the GUI thread I created a std::function object that captures shared_from_this() pointer. After that the std::function object was std::moved several times by another thread (but the object has never been copied). And finally, the std::function…

Mikhail Krasnorutsky
- 111
- 8
0
votes
0 answers
Using std::move over ofstream
While trying some mutex-locked print function, I got an idea like this:
class MyWriteStream
{
public:
MyWriteStream(std::ostream &stream) : stream_(std::move(stream)) {}
void write(const char *str, const size_t size)
{
…

user2338150
- 479
- 5
- 14
0
votes
1 answer
Assign RepeatedPtrField to repeated field in a protobuf message
I have a RepeatedPtrField and a protobuf message M as:
message M {
message Table {
optional string guid = 1;
optional int64 schema_version = 2;
optional int64 data_version = 3;
repeated Column column = 4;
}
repeated…

v78
- 2,803
- 21
- 44
0
votes
0 answers
Variable pointing on empty space while passing aguments using std::move from std::optional buffer
Trying to write simple code to simulate parsing packages in the factory.
I am sending a package which is saved in the buffer like:
sending class:
class PackageSender {
public:
void send_package();
std::optional…

smakethunter
- 14
- 2
0
votes
1 answer
std::swap between std::shared_ptr where A has dynamic array
first, my code:
struct A {
A(int size);
~A();
A(A&& other);
A& operator=(const A& other);
uint8_t* data = nullptr;
};
A::A(int size)
{
data = new uint8_t[size];
}
A::~A()
{
delete [] data;
data = nullptr;
}
A::A(TPixel…

Schrami
- 89
- 1
- 12
0
votes
2 answers
Assigning rvalue references while assigning string
I am learning about rvalue references and my brain is probably all over the place. But I wanted to test the std::move and rvalue references and below is the code snippet
string s1 = "hello";
{
string&& s2 = std::move(s1);
cout << s2 <<…

Kraken
- 23,393
- 37
- 102
- 162
0
votes
2 answers
Unable to bind function to std::function when passing a std::move() object as a function argument
Trying to compile this following snippet of code:
#include
#include
#include
void print_num(std::promise&& result, int i )
{
std::cout << i << " " << '\n' ;
result.set_value(true);
}
int main()
{
…

user2364011
- 71
- 1
- 6
0
votes
2 answers
Is it legal to assign to std::function that was moved
Is the following code legal:
std::function CreateFunction(int i, std::function previous_f) {
return [i,previous_f] {
std::cout << i << std::endl;
previous_f();
};
}
int main()
{
std::function f = []{};
…

Ilya Kobelevskiy
- 5,245
- 4
- 24
- 41
0
votes
1 answer
When I do int a = std::move(b) (b is int also), is it same as just a = b?
When I do int a = std::move(b) (b is int also), is it same as just a = b?

Wei Ding
- 31
- 1
- 3
0
votes
2 answers
Return an object with std::move and chain the function
I made a method that returns an object in that way:
MyObject &&
MyController::getMyObject (const otherObject & options) const
{
MyObject tmp;
tmp.doSometing(options);
return std::move(tmp);
}
Later in my code, I wanted to use that…

Sébastien Bémelmans
- 869
- 2
- 8
- 17
0
votes
1 answer
std::move with inner objects - no match for call
Code below doesn't compile.
Main:
#include "preset.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
SomeA a1(4);
WrapA wA1(a1);
WrapA wA2(std::move(wA1)); //fails to compile here
return a.exec();
}
preset.h…

Greadimar
- 81
- 8
0
votes
0 answers
Confusing error in initializer list using std::move on unique_pointers
I'm getting this error from this constructor
Severity Code Description Project File Line Suppression State Error
(active) E0289 no instance of constructor "std::unique_ptr<_Ty,
_Dx>::unique_ptr [with _Ty=InputComponent,…

Wesley Richmond
- 11
- 4
0
votes
2 answers
std::tuple and move semantic
I wonder about two things.
1. Is moving std::tuple worth implementing? For example for std::tuple would we gain anything? Would it be faster than copying or passing by reference?
2. In example given below, is there any real difference…

Chris
- 839
- 1
- 12
- 30