public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Brian Budge <brian.budge@gmail.com>
To: GCC-help <gcc-help@gcc.gnu.org>
Subject: potential bug regarding rvalue refs?
Date: Wed, 29 Jan 2014 18:58:00 -0000	[thread overview]
Message-ID: <CANjXV6_d5pSxtRzFh8urwuEq-VPft=3THkGxgCD+wNL6rP8Q7Q@mail.gmail.com> (raw)

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

             reply	other threads:[~2014-01-29 18:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-29 18:58 Brian Budge [this message]
2014-01-29 19:40 ` Florian Weimer
2014-01-29 20:10 ` Jonathan Wakely
2014-01-29 20:56   ` Brian Budge

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CANjXV6_d5pSxtRzFh8urwuEq-VPft=3THkGxgCD+wNL6rP8Q7Q@mail.gmail.com' \
    --to=brian.budge@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).