Why did initializer-list not value-initialize this struct?

129 views
Skip to first unread message

xml...@gmail.com

unread,
Dec 16, 2012, 1:21:06 PM12/16/12
to std-dis...@isocpp.org

The C++11 standard 8.5.4.3 says:

"If the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized."

struct A
{
    int get() { return i; }

private:
    int i;
};

int main()
{
    A a = {};

    int n = a.get();
    // Here I can watch a.i is not value-initialized, 
    // instead is 0xCCCCCCCC when running in debugging mode.

    return 0;
}

Is this a bug of VC++? My VC++ is the latest Nov 2012 CTP.

Daniel Krügler

unread,
Dec 16, 2012, 2:07:33 PM12/16/12
to std-dis...@isocpp.org
2012/12/16 <xml...@gmail.com>:
> The C++11 standard 8.5.4.3 says:
>
> "If the initializer list has no elements and T is a class type with a
> default constructor, the object is value-initialized."

Yes.

> struct A
> {
> int get() { return i; }
>
> private:
> int i;
> };
>
> int main()
> {
> A a = {};
>
> int n = a.get();
> // Here I can watch a.i is not value-initialized,
> // instead is 0xCCCCCCCC when running in debugging mode.
>
> return 0;
> }
>
> Is this a bug of VC++? My VC++ is the latest Nov 2012 CTP.

This looks like a compiler bug to me.

- Daniel

xml...@gmail.com

unread,
Dec 16, 2012, 3:35:17 PM12/16/12
to std-dis...@isocpp.org
But GCC behaves like VC++. Both are stupid?

Daniel Krügler

unread,
Dec 16, 2012, 3:37:54 PM12/16/12
to std-dis...@isocpp.org
2012/12/16 <xml...@gmail.com>:
> But GCC behaves like VC++. Both are stupid?

I'm running here gcc 4.8.0 20121209 (experimental) and it behaves as expected.

- Daniel

Klaim - Joël Lamotte

unread,
Dec 16, 2012, 6:42:08 PM12/16/12
to std-dis...@isocpp.org
Doesn't the value initialization applies to A? 
Here the default constructors isn't user defined so it's using the default constructor which will never call the int constructor anyway.

Shouldn't
A a = {}; 
be equivalent to 
A a = A(); 
?

Joel Lamotte

Klaim - Joël Lamotte

unread,
Dec 16, 2012, 7:16:23 PM12/16/12
to std-dis...@isocpp.org
Ignore my previous remark, I misread the cited standard sentence.

Joel Lamotte

luca ciciriello

unread,
Dec 16, 2012, 2:11:41 PM12/16/12
to std-dis...@isocpp.org
I think that is a visualisation problem of the debugger.
I've tried this code with LLVM clang 3.1 and LLDB debugger. With the debugger I see a uninitialized value of n, but if I print n out with cout << n << endl; on the the standard output I can see n = 0

Luca

2012/12/16 <xml...@gmail.com>

--
 
 
 

Mateusz Loskot

unread,
Dec 29, 2012, 4:52:17 PM12/29/12
to std-dis...@isocpp.org
On 16 December 2012 18:21, <xml...@gmail.com> wrote:
> struct A
> {
> int get() { return i; }
>
> private:
> int i;
> };
>
> int main()
> {
> A a = {};
>
> int n = a.get();
> // Here I can watch a.i is not value-initialized,
> // instead is 0xCCCCCCCC when running in debugging mode.
>
> return 0;
> }
>
> Is this a bug of VC++? My VC++ is the latest Nov 2012 CTP.


Does this example compile for you at all?
What is the exact version of the Visual C++ compiler?

I'm using the version 17.00.51106.1which throws the following:

error C2552: 'a' : non-aggregates cannot be initialized with initializer list
'A' : Types with private or protected data members are not aggregate

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
Reply all
Reply to author
Forward
0 new messages