Bug 85691 - Faulty Class Member Default Initialization - No Warning or Error
Summary: Faulty Class Member Default Initialization - No Warning or Error
Status: RESOLVED DUPLICATE of bug 19808
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2018-05-08 01:08 UTC by Roger Weber
Modified: 2018-05-08 11:25 UTC (History)
1 user (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Roger Weber 2018-05-08 01:08:44 UTC
/// Bug Report
struct test
	int x = data;
	int data;

	test(int o) : data(o) { }

/// Potentially Related Issue
struct test2
	int x;
	int data;

	test2(int o) : data(o), x(data) { }

- Details -
There should be a warning/error for test and test2.
In test the default initialization of x relies on data, which is initialized after x.
In test2 the constructor initialization list is out of order. Same result as for test.

- Note -
clang displays a warning for both these cases, but neither gcc nor msvc do
Comment 1 Jonathan Wakely 2018-05-08 11:25:05 UTC
Did you forget to enable warnings? GCC warns about test2 but not test:

$ g++ -Wall -c 85691.cc
85691.cc: In constructor ‘test2::test2(int)’:
85691.cc:14:13: warning: ‘test2::data’ will be initialized after [-Wreorder]
         int data;
85691.cc:13:13: warning:   ‘int test2::x’ [-Wreorder]
         int x;
85691.cc:16:9: warning:   when initialized here [-Wreorder]
         test2(int o) : data(o), x(data) { }

Clang has the same behaviour for -Wreorder, but it additionally warns about test::data with -Wuninitialized:

85691.cc:4:17: warning: field 'data' is uninitialized when used here [-Wuninitialized]
        int x = data;
85691.cc:7:9: note: during field initialization in this constructor
        test(int o) : data(o) { }
85691.cc:16:24: warning: field 'data' will be initialized after field 'x' [-Wreorder]
        test2(int o) : data(o), x(data) { }
85691.cc:16:35: warning: field 'data' is uninitialized when used here [-Wuninitialized]
        test2(int o) : data(o), x(data) { }
3 warnings generated.

We already have Bug 19808 covering this.

*** This bug has been marked as a duplicate of bug 19808 ***