2,175 questions
3
votes
2
answers
124
views
Moving underlying container (std::vector or std::deque) for std::stack and std::queue [duplicate]
I had a usecase where I use a stack to process some data. Once processed, I want to output the data as a vector. But since underlying containers in stack are protected, it is now allowed to:
stack<...
2
votes
2
answers
123
views
Why can I not efficiently move the strings when using std::istream_iterator<std::string>?
#include <fstream>
#include <string>
#include <vector>
int main() {
auto fin = std::ifstream("tmp.txt");
auto pos = std::istream_iterator<std::string>(fin);...
1
vote
2
answers
101
views
making an invocable concept more accurate with respect to argument value category
As far as I understand, std::invocable<callable_type,args_type...> treats the callable arguments as rvalue references to the types args_type.
As a consequence, std::invocable<void(std::...
4
votes
1
answer
94
views
Clang-tidy bugprone-use-after-move with perfect forwarding
clang-tidy reports the following piece of code as bugprone-use-after-move
template <typename F, typename Tuple, size_t... I>
auto transform_tuple_impl(F&& f, Tuple&& tuple, std::...
2
votes
2
answers
134
views
Value categories in C++: What does "can be moved from" mean?
In C++, the value category of an expression is determined by two independent properties:
Whether the expression has an identity
Whether the expression can be moved from
(References: cppreference and ...
6
votes
1
answer
469
views
Is const T& effectively the same as T&& with std::forward for rvalue argument when be passed into another function?
Say I have a const T& version:
template <typename T>
void yetAnotherFunc( const T &input ) {
// do something...
}
template <typename T>
void myFunc( const T &input ) {
...
-1
votes
1
answer
68
views
How do I move a Sender object out of a mutable reference to a Vector of tuples of Strings and Sender Objects?
This is not a solution to my problem. The suggested answer replaces the values with Nones. I quite literally require the size of my vector to reduce.
MRE:
use tokio::sync::mpsc;
#[tokio::main]
async ...
1
vote
0
answers
94
views
Why is the string& overload called instead of string&&? [duplicate]
Why is the output of the below code a reference: foo and not moved: foo?
#include <iostream>
void process(std::string& v) {
std::cout << "reference: " << v <<...
4
votes
3
answers
192
views
Copy constructor called when pushing object onto std::vector
#include <iostream>
#include <vector>
class Car{
public:
int weight;
Car(int weight): weight(weight){
};
Car(const Car& other){
std::cout<<&...
1
vote
0
answers
88
views
In my custom string class, can I have a special behavior if `substr` assigns to self?
I have a very simple fixed-buffer string class. I am targeting embedded and don't want dynamic allocations. I am including it for reference, but the question is more general.
template <auto ...
1
vote
3
answers
91
views
How not to capture or move a String in a match branch or ok_or argument
I'm working on a function that converts a json value into an i32, returning an error if any step of the conversion fails.
fn json_to_block_height(height: Value) -> Result<i32, RPCError> {
...
2
votes
0
answers
103
views
Is there a reason for the lack of vector's (and other sequential containers such as deque) constructor that moves an element to perfom one less copy?
Inspecting the constructor std::vector::vector( size_type count, const T& value, const Allocator& alloc = Allocator() );(ref), we can read that it:
Constructs a vector with count copies of ...
3
votes
1
answer
85
views
Understanding why the move constructor is called in addition to the move assignment operator in Stroustrup's PPP?
I am reading through Programming: Principles and Practice in C++, 4th edition by Stroustrup and I am confused about the output of some code that is intended to illustrate the copy constructor/copy ...
5
votes
2
answers
474
views
Warning "bugprone-exception-escape" when captured object throws in copy constructor
When I have a C++ lambda that captures an object by value, and that object can throw an exception in its copy constructor, clang-tidy will show a warning:
warning: an exception may be thrown in ...
-1
votes
2
answers
223
views
How to use move semantics in a loop and avoid copying?
I created a little parser and would like to make the looped part as fast as possible. Thus I would like to avoid copying using move semantics instead. But I can't understand how to combine move ...
1
vote
2
answers
112
views
Extracting a std::unique_ptr member using std::move(), from a class that is invalid when null: how to avoid invisibly making object invalid?
Consider the following scenario.
We have a class bar that owns a foo data object via a unique pointer. A bar must always be constructible into a valid state; that is, bar must always own some data, so ...
1
vote
1
answer
143
views
move assignment argument throws error when dereferenced but works when member accessed directly
In the minimum needed code snippet, in the move assignment, why is the commented line *arg = nullptr; illegal and arg.p = nullptr; okay? If I understand correctly, both are modifying rvalue, yet on ...
4
votes
2
answers
261
views
Move objects from a set to another set with a different comparison functor
I have a large number of large objects in my program. They are currently stored in an std::set with a customer comparison functor. The set starts empty and I keep emplacing objects into it. I also ...
3
votes
2
answers
177
views
Use after move in function call
When exactly does an object cast with std::moved get moved?
For instance, does the following code constitute use after move?
f(std::move(a), a.Something())
Where f = f(A a, int x) and a is an ...
2
votes
1
answer
468
views
Does Rust's move semantics involve copying data? [duplicate]
I am curious about the "move semantics" in Rust and whether data is copied when ownership is transferred. Here's some demo code:
#[derive(Debug)]
struct Foo {
name: i32,
age: i32,
}
...
2
votes
0
answers
92
views
Returning a moved parameter on failure
Is it an acceptable pattern to return a moved function parameter in case some kind of problem occurred? For example when trying to store data in a database, but validation failed and the data handle ...
0
votes
0
answers
67
views
Move semantic: Why does the return statement call the copy constructor, when the parameter is an rvalue reference? [duplicate]
Consider the following code:
class Widget {
public:
Widget() {
std::cout << "\nCtor";
}
Widget(const Widget& obj) {
std::cout << "\nCopy ctor&...
0
votes
0
answers
110
views
Does the Copy and Swap idiom have a not-required swap?
In an answer to this question, under the label: "Why does that work?" , it was noted that:
Now, if other is being initialized with an rvalue, it will be move-constructed. Perfect. In the ...
0
votes
2
answers
139
views
Is there a standard function for performing a move-or-copy in c++?
I have a function which takes an rvalue reference:
void foo(int&& x);
I would like to create another function which supplies a universal interface with the original function:
template <...
1
vote
1
answer
125
views
C++ passing a string to class setter function [duplicate]
I see this subject was discussed in StackOverflow, but I could not find the right answer.
I've been using C++ for many years, and I am still in doubt about how to write a simple setter method for a ...