How to perfect forward a member variable

As explained by @StoryTeller, if you forward the struct then the member value catogory is preserved (I think this is important to keep in mind). But how do I get the correct type when forwarding a member? you could do something like this:

template <typename T>
void foo(T&& some_struct) {
    bar(std::forward<decltype(std::declval<T>().member)>(some_struct.member));
}

In this code the actual object that's being forwarded is the member and not the object. As forwarding is a just conditional move, you might want to look at this two articles: enter link description here enter link description here


Member access does the right thing here: you just need std::forward<T>(some_struct).member.

Tested with:

template <class... >
struct check;

struct Foo {
    int i;
};

template <class T>
void bar(T &&f) {
    // fatal error: implicit instantiation of undefined template 'check<int &&>'
    check<decltype((std::forward<T>(f).i))>{};
}

int main() {
    bar(Foo{42});
}

Member access is value category preserving. If the object expression is an lvalue, so is the member access, otherwise it is an xvalue (like the result of std::move). So do member access on the result of forwarding the object.

std::forward<T>(some_struct).member