https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69901
Bug ID: 69901
Summary: Iniitializing non-const global array variable from
runtime const global variable does not copy all values
properly
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: piotrwn1 at gmail dot com
Target Milestone: ---
We discovered this in our project working under gcc4.9.2 (-std=c++14) but it is
also present in gcc5.2 - whilst not present in clang.
The code below does not copy b1 to b2 properly:
- b2[0].c.a is 0
- b1.c.a
Making b1 compiler time constant (by making makeA() function constexpr) "fixes"
the problem:
#include <iostream>
struct A
{
int a;
};
struct B
{
int b;
A c;
};
/*constexpr*/ //<-- contstexpr here "fixes" the problem
A makeA()
{
A a = {2};
return a;
}
const B b1 = {1, makeA()};
B b2[] = { b1 };
int main()
{
std::cout << b2[0].c.a << std::endl; // 0 (should be 2)
std::cout << b1.c.a << std::endl; // 2
}
The link to coliru: http://coliru.stacked-crooked.com/a/4f1ef718fea4e76f
The original example was with std::vector and much more complicated structs. We
did an effort to simplify example. Simplifying more also "fixes" the problem.
BR,
Piotr Nycz (piotrwn1(at)gmail.com)