https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77370

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2017-08-22
                 CC|                            |egallager at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #1 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #0)
> GCC rejects the program below because it tries to initialize the non-const
> reference object cr with a temporary, but it doesn't diagnose the
> declaration of the reference type CR where it disregards the const
> qualifier.  In contrast, Clang diagnoses both.  GCC could be improved by
> doing the same.
> 
> $ (set -x && cat z.C && /build/gcc-trunk-svn/gcc/xgcc -B
> /build/gcc-trunk-svn/gcc -Wall -Wextra -Wpedantic z.C;
> /build/llvm-trunk/bin/clang -S -Wall z.C)
> + cat z.C
> typedef char& R;
> typedef const R CR;
> 
> char c;
> R r = c;
> CR cr = 'b';
> + /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -Wall -Wextra
> -Wpedantic z.C
> z.C:6:9: error: cannot bind non-const lvalue reference of type ‘CR {aka
> char&}’ to an rvalue of type ‘char’
>  CR cr = 'b';
>          ^~~
> + /build/llvm-trunk/bin/clang -S -Wall z.C
> z.C:2:9: warning: 'const' qualifier on reference type 'R' (aka 'char &') has
> no
>       effect [-Wignored-qualifiers]
> typedef const R CR;
>         ^~~~~~
> z.C:6:4: error: non-const lvalue reference to type 'char' cannot bind to a
>       temporary of type 'char'
> CR cr = 'b';
>    ^    ~~~
> 1 warning and 1 error generated.

My version of clang only prints the one error, but then again my version of
clang is old (because my computer is old). Confirmed anyways that it'd still be
a nice additional warning to have.

$ /usr/local/bin/g++ -c -Wall -Wextra -pedantic 77370.cc
77370.cc:6:9: error: cannot bind non-const lvalue reference of type ‘CR {aka
char&}’ to an rvalue of type ‘char’
 CR cr = 'b';
         ^~~
$ /sw/opt/llvm-3.1/bin/clang++ -c -S -Wall -Wextra -pedantic 77370.cc
77370.cc:6:4: error: non-const lvalue reference to type 'char' cannot bind to a
temporary of type 'char'
CR cr = 'b';
   ^    ~~~
1 error generated.
$

Reply via email to