Skip to main content
Filter by
Sorted by
Tagged with
2 votes
1 answer
196 views

I need a list of unique pointers to ints (I need the underlying int arrays because of MPI compatibility, they are buffers that are getting tracked for work done). I have a workaround but would like to ...
Snark's user avatar
  • 1,696
3 votes
2 answers
125 views

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<...
Dwij Dixit's user avatar
1 vote
1 answer
232 views

While checking &other != this is generally not a good idea in move/copy assignment and the copy-and-swap idiom should be preferred, is it allowed for a swap to perform such a check? Copy-and-swap ...
Dominik Kaszewski's user avatar
2 votes
2 answers
125 views

#include <fstream> #include <string> #include <vector> int main() { auto fin = std::ifstream("tmp.txt"); auto pos = std::istream_iterator<std::string>(fin);...
xmllmx's user avatar
  • 44.6k
2 votes
1 answer
144 views

I'm studying std::move semantics and I would like this to be clarified to me: Say I have: // Message.h class Message { private: std::array<uint8_t, BUFFER_SIZE> buffer; std::queue<...
desynchedneo's user avatar
1 vote
2 answers
101 views

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::...
Oersted's user avatar
  • 3,904
5 votes
2 answers
181 views

Below is the most common implementation of std::swap: template<typename T> void std::swap(T& a, T& b) { auto tmp = std::move(a); a = std::move(b); b = std::move(...
xmllmx's user avatar
  • 44.6k
4 votes
1 answer
113 views

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::...
Stégosaure's user avatar
2 votes
0 answers
116 views

I'm experimenting, with wrapping move semantics into a wrapper type: #include <utility> #include <type_traits> // Type that will get moved struct NonDestructible { ~NonDestructible() =...
Dimo Markov's user avatar
2 votes
2 answers
139 views

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 ...
toliveira's user avatar
  • 1,867
3 votes
1 answer
95 views

I have an issue with clang-tidys recommendation Clang-tidy: arg is passed by value and only used once; consider moving it. So the code is essentially this: struct S{ kinda_heavy obj; S(...
Captain Giraffe's user avatar
6 votes
1 answer
471 views

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 ) { ...
PkDrew's user avatar
  • 2,301
6 votes
1 answer
227 views

I'm experimenting with move semantics in C++, and I have a simple class Entity that should only be movable — copying is disabled. Here’s the class: class Entity { public: //... Entity() = ...
Akram Gharbi's user avatar
-1 votes
1 answer
70 views

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 ...
kesarling's user avatar
  • 2,334
26 votes
3 answers
2k views

In the following code: struct copy_only { copy_only() = default; copy_only(const copy_only&) = default; copy_only& operator=(const copy_only&) = default; ...
Fantastic Mr Fox's user avatar
1 vote
0 answers
94 views

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 <<...
Jacob Krieg's user avatar
  • 3,292
3 votes
1 answer
136 views

Given the following code that tries to allocate a lambda capture on the heap: #include <array> int test(void) { //auto big_lambda = new auto([d = std::array<int, 1024>{5,1,2}]() { ...
Fullaccess 's user avatar
4 votes
3 answers
207 views

#include <iostream> #include <vector> class Car{ public: int weight; Car(int weight): weight(weight){ }; Car(const Car& other){ std::cout<<&...
simply lemon's user avatar
6 votes
2 answers
227 views

In C++, a moved object is left in a valid but unspecified state, which is basically a way to say you can assign to it and destroy it. But, can the object also be copied and moved? For example, if I ...
ABu's user avatar
  • 12.5k
1 vote
0 answers
88 views

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 ...
Tomáš Zato's user avatar
4 votes
1 answer
205 views

The following code: #include <chrono> #include <expected> #include <stdexcept> #include <string> #include <thread> #include <utility> using namespace std::literals;...
Adam Barnes's user avatar
  • 3,303
1 vote
3 answers
93 views

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> { ...
PiRK's user avatar
  • 1,105
2 votes
1 answer
158 views

So say I have a tree structure, with a node defined like: struct Node { byte key_part; unique_ptr<Node> left, right, middle; TValue value; }; unique_ptr<Node> root; The ...
Blindy's user avatar
  • 68k
2 votes
0 answers
103 views

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 ...
Fureeish's user avatar
  • 13.5k
2 votes
1 answer
98 views

In the following example the function Table::get_subtable() is returning a const reference to an internal subobject (SubTable). This value is then std::moved and passed into a static factory function ...
glades's user avatar
  • 5,392
3 votes
1 answer
85 views

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 ...
Sprotte's user avatar
  • 201
3 votes
2 answers
144 views

Is the following Move operations legit, as in there is no UB. Instead of two swaps I want to implement a one swap of the 16 Bytes of MyString. class MyString { std::size_t size_; char* str_; ...
dwto's user avatar
  • 827
5 votes
2 answers
510 views

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 ...
oliver's user avatar
  • 6,890
0 votes
1 answer
155 views

Let's say I have a function that returns a std::vector, and I want to call it asynchronously. std::vector<int> foo(int n); int main() { auto future_vector = std::async(foo, 999); // ... ...
Adrian McCarthy's user avatar
-1 votes
2 answers
236 views

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 ...
Max Cury's user avatar
1 vote
0 answers
77 views

I'm reading the "C++20 for Programmers An Objects-Natural Approach, 3rd Edition" and came across a paragraph in the book that discusses several features of std::jthread, specifically: "...
sam's user avatar
  • 911
1 vote
2 answers
112 views

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 ...
Involute's user avatar
  • 245
6 votes
0 answers
62 views

In the following code, which of the assignment to handler is correct or are they both equivalent to each other #include <string> #include <functional> struct mything { using handler_t =...
Penny Dreudter's user avatar
1 vote
1 answer
147 views

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 ...
Bruno Mraz's user avatar
1 vote
1 answer
135 views

I am trying to understand value categories in C++ a bit better, and I encountered some code that got me a bit confused. I looked at some questions such as Accept move-only parameter by value or rvalue ...
Francisco Castanheira's user avatar
4 votes
2 answers
274 views

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 ...
Alberto Santini's user avatar
2 votes
2 answers
185 views

I am playing around with C++ and faced this problem. If I have a variant that looks like this: using Fruit = variant<Apple, Tomato> and a vector of unique_ptr of Fruit: vector<unique_ptr<...
HiddenPasta's user avatar
3 votes
2 answers
178 views

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 ...
c z's user avatar
  • 9,355
1 vote
1 answer
57 views

When I move a boost::type_erasure::any object like in this test case: #include <boost/type_erasure/any.hpp> #include <string> int main() { typedef ::boost::mpl::vector< ...
Stefan v.K.'s user avatar
2 votes
1 answer
497 views

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, } ...
Archsx's user avatar
  • 1,052
0 votes
1 answer
99 views

I have this code but I do not understand why it compiles: use std::thread; use std::time::Duration; struct State { value: u32, } impl State{ pub fn new() -> State{ State{ value: 0 }...
Gerhard77's user avatar
2 votes
0 answers
92 views

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 ...
Michael's user avatar
  • 958
6 votes
2 answers
253 views

This question is about how to implement assignment in modern C++ value types, with the goal of avoiding misleading code or code that is inconsistent with built-in behavior. In C++, generated values (...
alfC's user avatar
  • 16.8k
0 votes
0 answers
69 views

Consider the following code: class Widget { public: Widget() { std::cout << "\nCtor"; } Widget(const Widget& obj) { std::cout << "\nCopy ctor&...
exect's user avatar
  • 1
3 votes
2 answers
586 views

In term of move semantics on return value, I heard that the best practice is to return the object as is, and don't call std::move to it, as it prevent return value optimizations. However, consider the ...
lewisxy's user avatar
  • 353
0 votes
0 answers
114 views

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 ...
CS Student's user avatar
14 votes
2 answers
584 views

Simplified program as follows struct S { S() {} S(const S &) {} S(S &&) = delete; }; S x; S y = false ? S() : x; is accepted just fine by GCC and Clang, but the latest Visual ...
Fedor's user avatar
  • 24.8k
0 votes
2 answers
139 views

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 <...
Arc_Angle's user avatar
1 vote
2 answers
133 views

Consider the following code: fn print_or(opt: Option<()>, tail: Vec<i32>) -> Vec<i32> { opt.map_or(tail, |_| { println!("{:?}", tail); tail }) } ...
Some Name's user avatar
  • 9,750
1 vote
1 answer
129 views

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 ...
Andrey Rubliov's user avatar

1
2 3 4 5
44