开发者

Do Ideone and Codepad really not support C++03?

开发者 https://www.devze.com 2023-01-24 03:19 出处:网络
My question is related to Prasoon\'s question about non POD types and value initialization. I tried the following code on online compilers like 开发者_开发百科Ideone and Codepad but the executables g

My question is related to Prasoon's question about non POD types and value initialization.

I tried the following code on online compilers like 开发者_开发百科Ideone and Codepad but the executables gave runtime error on both the sites.

#include <iostream>
#include <cassert>

struct Struct {
    std::string String;
    int Int;
    bool k;
};

struct InStruct:Struct
{
   InStruct():Struct(){}
};

int main()
{
   InStruct i;
   assert ( i.Int == 0);
   std::cout << "Hello";
}

Ideone Output here

Codepad Output here

Does that mean neither of them support C++03 value initialization feature?


Does that mean neither of them support C++03 value initialization feature?

Yes.

Prior to version 4.4, GCC did not completely support value initialization (the Boost GCC compatibility header explains this and has links to the relevant GCC defect reports; see line 77).

If your code needs to be portable, you should be very careful relying on value initialization; GCC did not support it fully until recently and Visual C++ does not fully support it even in its latest version, Visual C++ 2010.


The declaration

InStruct i; 

does not invoke value initialization

$8.5.3/10 - "An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized."

If you want to value-initialize, you would require an expression something like

assert(InStruct().Int == 0);


Try it now! - Ideone supports GCC-4.5.1

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号