potential bug regarding rvalue refs?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux