https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64842
Bug ID: 64842
Summary: Implicitly defined constructor isn't constexpr
Product: gcc
Version: 4.9.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: simendsjo at simendsjo dot me
The following short example is a (somewhat reduced) example from
Bjarne Stroustroups book Programming Principles and Practice using
C++ 2nd edition first print, section 8.5.9 on page 290.
I'm able to reproduce the behavior on shorter examples, but I'm
afraid to do so as I don't know enough C++ to know if I'm hiding
the actual error.
After mailing Stroustrup about the issue, he suggested I added
a bug report.
He noted that I should copy the following text in the bugreport:
12.8 Copying and moving class objects [class.copy]
A copy/move constructor that is defaulted and not defined as
deleted is implicitly defined if it is odrused (3.2) or when
it is explicitly defaulted after its first declaration.
[ Note: The copy/move constructor is implicitly defined even
if the implementation elided its odr-use (3.2, 12.2). —end
note ] If the implicitlydefined constructor would satisfy
the requirements of a constexpr constructor (7.1.5), the
implicitly-defined constructor is constexpr.
The code in question that fails to compile (a bit reduced from the
book example):
struct Point { double x, y; };
constexpr double xscale = 10;
constexpr double yscale = 0.8;
constexpr Point scale(Point p) { return
{xscale*p.x,yscale*p.y};}
int main() {
Point p2 {10,10};
constexpr Point p6 = scale(p2); // Error 'p2 not usable
in constant expression'
}
The compiler arguments used and the error it produces:
$ g++ -std=c++11 -c -o test.o test.cpp
test.cpp: In function ‘int main()’:
test.cpp:10:34: error: the value of ‘p2’ is not usable in a
constant expression
constexpr Point p6 = scale(p2); // Error 'p2 not usable
in constant expression'
^
test.cpp:9:11: note: ‘p2’ was not declared ‘constexpr’
Point p2 {10,10};
^
Versions used:
$ gcc --version
gcc (GCC) 4.9.2 20141224 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
$ g++ --version
g++ (GCC) 4.9.2 20141224 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
$ uname -a
Linux simendsjo-desktop 3.16.6-1-ck-bamboo #1 SMP PREEMPT Mon
Jan 5 20:15:19 CET 2015 x86_64 GNU/Linux
Let me know if you need more information in order to reproduce
this.