[Bug c++/77370] missing -Wignored-qualiifiers on a bogus const reference

egallager at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Oct 2 02:27:00 GMT 2018


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

--- Comment #2 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Eric Gallager from comment #1)
> (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.
> $

Update: On a newer computer with a newer version of clang I can confirm that I
get the additional warning from -Wignored-qualifiers too: 

$ clang++ -c -S -Wall -Wextra -pedantic 77370.cc
77370.cc:2:9: warning: 'const' qualifier on reference type 'R' (aka 'char &')
has no effect [-Wignored-qualifiers]
typedef const R CR;
        ^~~~~~
77370.cc: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.
$


More information about the Gcc-bugs mailing list