[Bug c++/69103] Misleading diagnostic for invalid constexpr initialization
egallager at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Mon Aug 21 17:00:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69103
Eric Gallager <egallager at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Last reconfirmed| |2017-08-21
CC| |egallager at gcc dot gnu.org
Ever confirmed|0 |1
--- Comment #2 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to TC from comment #0)
> Consider:
>
> static int i;
> static int* temp = &i;
> static constexpr int *&&r = static_cast<int*&&>(temp) + 1;
>
> This correctly doesn't compile, but produces the misleading error message
>
> prog.cc:3:57: error: modification of '_ZGRL1r0' is not a constant-expression
> static constexpr int *&&r = static_cast<int*&&>(temp) + 1;
> ^
The '_ZGRL1r0' is now '<temporary>':
$ /usr/local/bin/g++ -c -Wall -Wextra -pedantic 69103.cc
69103.cc:3:57: error: modification of ‘<temporary>’ is not a constant
expression
static constexpr int *&&r = static_cast<int*&&>(temp) + 1;
^
$
(In reply to Mikhail Maltsev from comment #1)
> Link to the original thread, for the record:
> https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/
> WS5pLaXwC8Y
>
> _ZGRL1r0 demangles as "reference temporary #0 for r"
Confirmed that demangling it like that would still be better though.
(In reply to TC from comment #0)
>
> We aren't modifying anything in the initializer.
>
> In contrast, Clang produces:
>
> prog.cc:3:25: error: constexpr variable 'r' must be initialized by a
> constant expression
> static constexpr int *&&r = static_cast<int*&&>(temp) + 1;
> ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> prog.cc:3:29: note: read of non-constexpr variable 'temp' is not allowed in
> a constant expression
> static constexpr int *&&r = static_cast<int*&&>(temp) + 1;
> ^
> prog.cc:2:13: note: declared here
> static int* temp = &i;
...and of course doing like clang here would be the best.
More information about the Gcc-bugs
mailing list