Uniform initialization of references

Viewed 1993

I am currently trying to understand the new uniform initialization of C++0x. Unfortunately, I stumpled over using uniform initialization of references. Example:

int main() {
   int a;
   int &ref{a};
}

This example works fine:

% LANG=C g++ uniform_init_of_ref.cpp -std=c++0x -o uni -Wall -Wextra
uniform_init_of_ref.cpp: In function `int main()':
uniform_init_of_ref.cpp:3:10: warning: unused variable `ref' [-Wunused-variable]

(Update Comeau throws an error for that example, so maybe gcc shouldn't compile it as well)

Now, if I use a custom data type instead of an integer, it doesn't work anymore:

class Y
{};

int main()
{
    Y y;
    Y &ref{y};
}

% LANG=C g++ initialization.cpp -std=c++0x -o initialization -Wall -Wextra
initialization.cpp: In function `int main()':
initialization.cpp:9:13: error: invalid initialization of non-const reference of type `Y&' from an rvalue of type `<brace-enclosed initializer list>'
initialization.cpp:9:8: warning: unused variable `ref' [-Wunused-variable]

Unfortunately, I didn't find the relevant section in the standard draft. My guess is that I am misunderstanding the usage of uniform initialization, as Comeau complains with this message:

ComeauTest.c(9): error: reference variable "ref" requires an initializer
      Y &ref{y};

So, can someone of you point me in the right direction?


In case that you want to know why this question is relevant and why I don't just use Y &ref(y): I'd like to be able to use uniform initialization in the initialization list of a constructor:

class X { };

class Y {
    const X& x;

    public:
        Y (const X& xx):
            x{xx}
        {}
};

int main () {
    X x;
    Y y{x};
}

This fails with the same error message as above.

Note:

  • I am using LANG=C to enable english error messages.
  • gcc version: 4.6.1
1 Answers
Related