c++0x compiler support

Hello,
I am trying to get rvalue references running, but it seems that it does only work with gcc version 4.4. Is this a known feature or am I doing something wrong?
Here is a small example program:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
void test(int x, int& y=(int){1}){
  y=x+y;
  std::cout<<y<<std::endl;
}
int main(){
  int x=1;  int y=2;
  test(x,y);
  test(x);
}

I am trying to compile with g++ -std=c++0x -o test test.cc

Thank You
Stefan
There are no rvalue references in your program, and it cannot be compiled because it attempts to bind an rvalue (the expression (int){1}, which, by the way, is C-only. The C++ syntax is int{1} or just 1) to a non-const lvalue reference (the parameter int& y))

Was your intention something like this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <utility>

void test(int x, int&& y=1){
  y=x+y;
  std::cout<<y<<std::endl;
}
int main(){
  int x=1;  int y=2;
  test(x, std::move(y));
  test(x);
}
Last edited on
Thank you, that works.
I got a code from a colleague, which was similar to that example.
I was already wondering about the thing with the braces, but thought that's how the new syntax looks like .
Topic archived. No new replies allowed.