• 9
• 9
• 10
• 10
• 9

# Quick r-value reference question

This topic is 451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. Just a quick query that you guys will answer in a snap I'm sure.

So I have a forwarding-constructor to a class. I wrote it like this:

template<typename... Args> BlendValue(Args... args) : v(args...) { }

But when I was peeking at the std::vector implementation that ships with my GCC, at std::vector::emplace_back, I saw they did it like this:

template<typename... Args> void emplace_back(Args&&... args); // double-& before the ellipses

I can't seem to find the implementation of std::vector::emplace_back so I can't then see how they implement the method but I was wondering what the difference was between the two. The way I wrote it originally seems to work okay, but I'm sure my standard library implementors must have their reasons.

Also, should I be doing something like:

template<typename... Args> BlendValue(Args... args) : v(std::forward(args...)) { }

or similar?

Thanks.

##### Share on other sites

Also, should I be doing something like: template BlendValue(Args... args) : v(std::forward(args...)) { } or similar?

The correct, perfect forwarding solution would be this:

template<typename... Args> BlendValue(Args&&... args) : v(std::forward<Args>(args)...) { }


I still can't give a good understandable explanation as to why you have to use Args&& myself, so I'll just point you to a blog if you are interested:

http://eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c/

(or just search for "perfect forwarding" and you should find lots of examples).

##### Share on other sites

Also, should I be doing something like: template BlendValue(Args... args) : v(std::forward(args...)) { } or similar?

The correct, perfect forwarding solution would be this:
template<typename... Args> BlendValue(Args&&... args) : v(std::forward<Args>(args)...) { }

I still can't give a good understandable explanation as to why you have to use Args&& myself, so I'll just point you to a blog if you are interested:

http://eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c/

(or just search for "perfect forwarding" and you should find lots of examples).

Simply put, because T&& is special (in contexts where T is deduced), but T alone is not.

Consider, for example, this:
template <typename T>
void foo(T);

…

int i = 4;
foo(i);
foo(5);
In both calls, T is deduced to be int – not int&, not int&&, just int. Therefore, the information about whether the function is called with an lvalue or an rvalue is lost there.

If, instead, you change foo to be
template <typename T>
void foo(T&&);
then T will be deduced to either int& (in the foo(i) call) or int&& (in the foo(5) call). Thus you retain the information about whether the function is called with an lvalue or an rvalue.

The article that you linked covers the details of how that works, so I won't repeat them here.

For parameter packs it's the same, except there's multiple of them.