150 questions
2 votes
1 answer
122 views
Is it ok to send a value to the lvalue argument using std::move?
I have a function of the form void foo(std::string str); I want to use it in two scenarios: send a copy of the original value to it: std::string myString = "any data that I want to keep in its ...
5 votes
1 answer
164 views
Does std::span<T> remain valid and point to the new std::vector<T> upon move of a std::vector<T>
If we have an initialized std::vector std::vector<T> v = ...; then construct a span into that vector std::span<T> s = std::span<T>(v.begin(), 10); and then move the original vector ...
0 votes
0 answers
125 views
std::move vs RVO under -O3
I played with a snippet of code below and had it compiled by GCC to compare std::move vs RVO( return value optimization ) #include <iostream> #include <chrono> #include <array> #...
1 vote
2 answers
197 views
C++ function returning by value can return rvalue reference?
I was reading up on rvalues and ran into something while trying a code snippet on compiler explorer. Here's a contrived example: class A { public: A&& func(A& rhs) { //...
-1 votes
2 answers
231 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 ...
2 votes
1 answer
166 views
overwrite std::vector<T> with another std::vector<T> efficiently
#include <iostream> #include <vector> #include <utility> // simple user-defined class class MyClass { public: MyClass(int val) : value(val) {} private: int value; }; int ...
2 votes
2 answers
164 views
If only the virtual destructor is declared as default, Is the copy constructor implicitly generated?
In the case of the example below Implicitly, the move constructor and move assignment operator are not created. I knew. So, are the copy constructor and copy assignment operator generated? Is the ...
1 vote
0 answers
41 views
std::move a boost::flat_map of unique_ptr [duplicate]
I want to be able to initialize a class that holds a boost::container::flat_map<std::string, std::unique_ptr<AbstractBaseClass>> in the constructor. Example: class MapHolder { public: ...
1 vote
1 answer
128 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 ...
0 votes
0 answers
174 views
MSVC constexpr error "a non-constant (sub-)expression was encountered (with no info) (works on Clang!)
While working on my own array types, I encountered this issue, where one of my unit tests passes for Clang, but fails on MSVC with the following messages: error C7595: 'UnitTest': call to immediate ...
0 votes
0 answers
55 views
compiler does not complain when I do std::move of a const reference [duplicate]
I'm trying to understand which is the most optimized way to pass arguments to functions. I got the answer for the book professional c++ but during some test I ran into a weird behavior. It's clear ...
0 votes
1 answer
117 views
How to erase an element from a vector using a std::move_iterator?
This is a Josephus Permutation problem, it was solved by using std::move(), but now I have to switch to using std::move_iterator (by using std::make_move_iterator()) However, this doesn't compile: ...
1 vote
1 answer
424 views
Lambda capture with std::move and this
I'm actually working to delete most of my copy constructor/assignation, and I faced an issue when comming to lambda use: ThreadPool: using Task = std::function<void()>; template<size_t S> ...
0 votes
1 answer
87 views
C++ - Interaction between move construction and forwarding
If I'm constructing a non-trivial container like a custom map for example, and want to share code between the emplace and insert functions, one approach would be to have the insert function simply ...
5 votes
1 answer
219 views
How does std::forward work in the context of a fold expression?
In the MSVC STL, the implementation of std::apply is as follows: template <class _Callable, _Tuple_like _Tuple, size_t... _Indices> constexpr decltype(auto) _Apply_impl(_Callable&& _Obj, ...