public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* potential bug regarding rvalue refs?
@ 2014-01-29 18:58 Brian Budge
  2014-01-29 19:40 ` Florian Weimer
  2014-01-29 20:10 ` Jonathan Wakely
  0 siblings, 2 replies; 4+ messages in thread
From: Brian Budge @ 2014-01-29 18:58 UTC (permalink / raw)
  To: GCC-help

Hi all -

I have been playing around with universal references and wanted to see
what the difference is between something like

template <typename T>
void f(T t) {
  f_impl(std::forward<foo>(t));
}

and

template <typename T>
void f(T &&t) {
  f_impl(std::forward<foo>(t));
}


I wrote a quick program to start to try and tease out any differences,
but found some totally unintuitive behaviour that I think may be a
bug?

#include <iostream>

#if 0
struct foo {
  int a;

  foo(int b) : a(b) {}
};

#else

typedef int foo;

#endif

void f_impl(foo const &a) {
  std::cerr << "f by const ref" << std::endl;
}

void f_impl(foo &&a) {
  std::cerr << "f by rvalue ref" << std::endl;
}

template <typename T>
void f(T t) {
  f_impl(std::forward<foo>(t));
}

void g_impl(foo const &a) {
  std::cerr << "g by const ref" << std::endl;
}

void g_impl(foo &&a) {
  std::cerr << "g by rvalue ref" << std::endl;
}

template <typename T>
void g(T &&t) {
  g_impl(std::forward<foo>(t));
}

int main(int argc, char **args) {

  foo lvalue = 5;

  f(lvalue);
  f(5);

  g(lvalue);
  g(5);

  return 0;
}


When I compile this via g++ 4.8.2, I get the following output:

:./a.out
f by rvalue ref
f by rvalue ref
g by rvalue ref
g by rvalue ref

Although I'm far from an expert with rvalue refs and universal refs,
this seems like a bug to me.  Thoughts?

Thanks,
  Brian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: potential bug regarding rvalue refs?
  2014-01-29 18:58 potential bug regarding rvalue refs? Brian Budge
@ 2014-01-29 19:40 ` Florian Weimer
  2014-01-29 20:10 ` Jonathan Wakely
  1 sibling, 0 replies; 4+ messages in thread
From: Florian Weimer @ 2014-01-29 19:40 UTC (permalink / raw)
  To: brian.budge; +Cc: gcc-help

On 01/29/2014 07:58 PM, Brian Budge wrote:

>    f_impl(std::forward<foo>(t));

You need to write std::forward<T> instead of std::forward<foo> to get 
the expected forwarding behavior.

> Although I'm far from an expert with rvalue refs and universal refs,
> this seems like a bug to me.  Thoughts?

What output do you expected instead?


-- 
Florian Weimer / Red Hat Product Security Team

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: potential bug regarding rvalue refs?
  2014-01-29 18:58 potential bug regarding rvalue refs? Brian Budge
  2014-01-29 19:40 ` Florian Weimer
@ 2014-01-29 20:10 ` Jonathan Wakely
  2014-01-29 20:56   ` Brian Budge
  1 sibling, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2014-01-29 20:10 UTC (permalink / raw)
  To: Brian Budge; +Cc: GCC-help

This is a question about C++ and not specific to G++ so not really
on-topic on this list.

On 29 January 2014 18:58, Brian Budge wrote:
>
> Although I'm far from an expert with rvalue refs and universal refs,
> this seems like a bug to me.  Thoughts?

G++ is correct, you're forwarding 't' as an rvalue, so overload
resolution chooses the function taking an rvalue reference.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: potential bug regarding rvalue refs?
  2014-01-29 20:10 ` Jonathan Wakely
@ 2014-01-29 20:56   ` Brian Budge
  0 siblings, 0 replies; 4+ messages in thread
From: Brian Budge @ 2014-01-29 20:56 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: GCC-help

On Wed, Jan 29, 2014 at 12:09 PM, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> This is a question about C++ and not specific to G++ so not really
> on-topic on this list.
>
> On 29 January 2014 18:58, Brian Budge wrote:
>>
>> Although I'm far from an expert with rvalue refs and universal refs,
>> this seems like a bug to me.  Thoughts?
>
> G++ is correct, you're forwarding 't' as an rvalue, so overload
> resolution chooses the function taking an rvalue reference.

Florian, Jonathan, thanks for the responses.

  Brian

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-01-29 20:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-29 18:58 potential bug regarding rvalue refs? Brian Budge
2014-01-29 19:40 ` Florian Weimer
2014-01-29 20:10 ` Jonathan Wakely
2014-01-29 20:56   ` Brian Budge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).