[C++0x] Question on list initialization of references

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

 



Hi,


I recently stumbled across the list initialization feature of C++0x and
tried to use it to initialize references. The
following compiles nicely using gcc (gcc 4.6.1, see the end of the
email for gcc -v):

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

If I try to use the same style of initialization for a reference to a
user-defined type, gcc reports an error:

--------------------
class Y
{ };

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

% LANG=C make initialization CXXFLAGS="-std=c++0x -Wall -Wextra"
g++ -std=c++0x -Wall -Wextra    initialization.cpp   -o initialization
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]


This error message makes sense with a certain interpretation of the
quoted paragraph from §8.5.4 (draft N3126):

[...] if T is a reference to class type or if T is any reference
type and the initializer list has no elements, a prvalue temporary of
the type referenced by T is list-initialized, and the reference is
bound to that temporary. [ Note: As usual, the binding will fail and
the program is ill-formed if the reference type is an lvalue reference
to a non-const type. — end note ]

I think this applies here for my user-defined type Y. But I am wondering
why the first example compiles. Even if there exists a "constructor" for
integers using references that could produce a temporary, the integer
reference should still produce an error as it would bind to an rvalue.


So my question is: Where am I wrong?


Regards, Magnus

% LANG=C gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/src/gcc-4.6.1/configure --prefix=/usr
--libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++
--enable-shared --enable-threads=posix --with-system-zlib
--enable-__cxa_atexit --disable-libunwind-exceptions
--enable-clocale=gnu --enable-gnu-unique-object
--enable-linker-build-id --with-ppl --enable-cloog-backend=isl
--enable-lto --enable-gold --enable-ld=default --enable-plugin
--with-plugin-ld=ld.gold --disable-multilib --disable-libstdcxx-pch
--enable-checking=release Thread model: posix gcc version 4.6.1 (GCC) 

Attachment: pgpRnXq98wBVT.pgp
Description: PGP signature


[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