I am having a hard time understanding why std::move()
with object literals doesn't work.
Given the following simple code:
#include <memory>
#include <vector>
class A{
};
void func(A &¶m1){
}
int main()
{
auto a = std::unique_ptr<A>(new A());
auto vec = std::vector<std::unique_ptr<A>>{std::move(a)}; //this code does not work!!
//std::vector<std::unique_ptr<A>> vec; // but this code works
//vec.push_back(std::move(a));
return 0;
}
Since C++20, push_back()
is defined as constexpr
:
constexpr void push_back( T&& value );
Shouldn't std::move()
work the same in both object literal vector initialization and using push_back()
?