Why is the Foo data member moved in this context?
#include <iostream>
struct Foo
{
    Foo() = default;
    Foo(Foo&&) { std::cout << "move!" << std::endl; }
    Foo(const Foo&) { std::cout << "copy!" << std::endl; }
};
struct Bar
{
    Foo foo{};
};
template<typename T>
void forward_data_member(T&& bar)
{
    const auto foo = std::forward<Foo>(bar.foo);
}
int main()
{
    Bar bar{};
    forward_data_member(bar); // move!
    return 0;
}
I've checked with the debugger - the type of bar is deduced to be an lvalue reference when forward_data_member is called here.
I would assume that a data member of an lvalue reference would also be an lvalue. In fact, it says here that a data member is always an rvalue reference - so I really don't understand this behaviour! https://en.cppreference.com/w/cpp/language/value_category
Any help much appreciated! :-)
Aucun commentaire:
Enregistrer un commentaire