Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perfect forward a member variable [duplicate]

Consider the following code:

template<typename T> void foo(T&& some_struct)
{
    bar(std::forward</* what to put here? */>(some_struct.member));
}

In the case of forwarding the whole struct I would do std::forward<T>(some_struct). But how do I get the correct type when forwarding a member?

One idea I had was using decltype(some_struct.member), but that seems to always yield the base type of that member (as defined in the struct definition).

like image 273
mic Avatar asked Feb 15 '19 12:02

mic


3 Answers

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
like image 142
StoryTeller - Unslander Monica Avatar answered Nov 03 '22 17:11

StoryTeller - Unslander Monica


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

like image 37
K.Kaland Avatar answered Nov 03 '22 17:11

K.Kaland


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});
}
like image 20
Quentin Avatar answered Nov 03 '22 15:11

Quentin