jwd
jwd

Reputation: 11114

Example of rvalue reference collapsing with "&& &&"

When you have templates and references, you can wind up with types like T & &&, which get collapsed, according to some rules. Basically, everything gets collapsed to T& except T && && which gets collapsed to T&&

What is a real-world example where the && && case is triggered?

I found one artificial example:

template <typename T> void func(T&& a);
auto fp = func<int&&>;

But in my research so far, I haven't gotten a good sense of when this case realistically comes up.

Upvotes: 3

Views: 197

Answers (1)

Forwarding when the parameter type is an unconstrained auto would be a real world example. For instance, in a lambda.

[](auto && arg) {
    return foobar(std::forward<decltype(arg)>(arg));
}

decltype(arg) would be some T&& when the lambda is called with an rvalue. The reference collapsing rules would ultimately kick in inside the implementation of std::forward and turn T && && into a T&& correctly for the return type.

Upvotes: 3

Related Questions