Jump to content
  • Advertisement
Sign in to follow this  

Perfect forwarding, further questions.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Thought better to start a new thread rather than continue my recent one since my question has changed slightly. Hope that is okay.

I was asking earlier about the difference between
template<typename... Args> void f(Args... args){ call(args...); }
template<typename... Args> void f(Args&&... args){ call(std::forward<Args>(args)...); }
For which I got a good response and a link to an article that sort of made sense. Upshot was that the second version was to be preferred.

So I went to update some of my library. First of all, I had a perfect forwarding constructor to a class which I changed and this works fine:
template<typename T> class BlendValue
    template<typename... Args> BlendValue(Args&&... args) : v(std::forward<Args>(args)...), o(v) { }
However, I also have a signals/slots implementation which tries to use perfect forwarding for the call of the signal. My previous version looked like this:
template<typename... Args> void Signal<Args...>::operator()(Args... args)
	for(auto &n: v) n->call(args...); // call back the slots
When I changed this to:
template<typename... Args> void Signal<Args...>::operator()(Args&&... args)
	for(auto &n: v) n->call(std::forward<Args>(args)...); // call back the slots
elsewhere in the library, where I declare a signal simply as:
Signal<bool> activated;

void ApplicationEvents::onActivated(bool state)
The compiler (GCC) is throwing an error:
cannot bind 'bool' lvalue to 'bool&&'
Now, when I use my original code, without the && and the std::forward<>, everything works. I've tested and I can create signals and trigger them with value types, reference types, even const reference types that can be given temporaries as the call parameter. So the original version works and does everything I want.

But it is all contrary to the expert advice I am being given, so I wondered if anyone could shed any light on the compiler error I am getting and explain what is happening here?

Thanks in advance.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!