potential bug regarding rvalue refs?

Brian Budge brian.budge@gmail.com
Wed Jan 29 18:58:00 GMT 2014


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



More information about the Gcc-help mailing list