Consider this example:
#include <vector>
void addRow(std::vector<std::vector<double>>& matrix, const std::vector<double>& row) {
matrix.push_back(std::move(row));
}
I would've expected issues with std::move
and a const &
as std::move
basically invalidates that reference since it's contents are moved and it is set to a clean state, and this is the exact behavior const
must prevent.
This was compiled using:
g++ -std=c++20 -Ofast *.cpp -o test
with no warnings or errors and runs fine.
I've looked at other posts: Move construction from const reference -> apparently this didn't use to work
Const reference VS move semantics -> no answer
Why can we use `std::move` on a `const` object? -> apparently it works on a const as well, though that's not concerning
What's concerning about the reference, is it's invalidating an object it's promising not to modify.
The mostly frustrating thing about my post being labeled a duplicate is that I linked the 'duplicate' and explicitly stated it didn't answer my question, also expressing the reason why. Thanks.