This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/78813] New: constexpr function returns wrong value
- From: "charles.frasch at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 14 Dec 2016 16:25:43 +0000
- Subject: [Bug c++/78813] New: constexpr function returns wrong value
- Auto-submitted: auto-generated
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78813
Bug ID: 78813
Summary: constexpr function returns wrong value
Product: gcc
Version: 6.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: charles.frasch at gmail dot com
Target Milestone: ---
Created attachment 40341
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40341&action=edit
source.ii
The code below fails to compile when the second static_assert fails.
source.cpp: In function ‘int main()’:
source.cpp:25:3: error: static assertion failed
static_assert(cx_convert("123", 3) == 123);
^~~~~~~~~~~~~
It compiles and the runtime produces the expected result when the second static
assert is commented out.
When the replacement code is used the second static assert passes while the
first fails as would be expected.
#include <cassert>
#include <cstdint>
constexpr std::int64_t
cx_convert(char const * const s, std::size_t const length)
{
std::size_t i = 0;
std::int64_t whole = 0;
while (i < length) {
whole *= 10;
whole += s[i++] - '0';
// Replace line above with this and expected value is returned
// whole += s[i] - '0'; i++;
}
return whole;
}
int main() {
// Passes!!!!!
static_assert(cx_convert("123", 3) == -28);
// Fails; cx_convert returns -28 as above
static_assert(cx_convert("123", 3) == 123);
// Passes at runtime if second static_assert is commented out.
assert(cx_convert("123", 3) == 123);
}
----------------
cfrasch@telx-sb-dev3:~/projects/theme_gcc62(feature/ticket_22439)$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/home/cfrasch/local/bin/../libexec/gcc/x86_64-linux-gnu/6.2.0/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../gcc-6.2.0/configure -v --enable-languages=c,c++
--disable-multilib --with-build-config=bootstrap-lto --program-suffix=-6.2
--enable-shared --without-included-gettext --enable-threads=posix --enable-nls
--enable-nls --enable-gnu-unique-object --with-system-zlib --enable-multiarch
--disable-werror --with-arch-32=i686 --with-abi=m64 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu --with-pkgversion=Thesys 6.2-1 ubuntu 14.04
Thread model: posix
gcc version 6.2.0 (Thesys)
Compile using: g++ -std=c++14 source.cpp