int a=int(); what happens in C++98?
Asked Answered
M

1

9

Please read the question entirely before you think to mark it as duplicate. The statement like

int i=int();

most programmers will say that there is value initialization here & i will be value initialized. (0 as output). But it also prints 0 as output on C++98 compiler. Following program that I tested on C++98 implementation and gives me 0 as output.

#include <iostream>
int main()
{
     int i=int();
     std::cout<<i;
}

Don't say that i is value initialized in above C++98 program ,because value initialization introduced in C++03. So How i is initialized here? Is it really constructor call? int() looks like constructor call. Primitive types have also default constructors in C++ as said by Bjarne stroustrup in his book C++ programming language & TC++PL.

The C++ programming language Bjarne stroustrup:

10.4.2 Built in types also have default constructors

also read section 6.2.8 of same book.

The following links also says that built in types have default constructors in C++.

1) http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15

2) http://www.geeksforgeeks.org/c-default-constructor-built-in-types/

So can I really say that it is a constructor call of the integer type?

Mennonite answered 2/6, 2015 at 18:11 Comment(8)
@vsoftco: How it is possible?Mennonite
No (comparatively recent) compiler I know of has an actual C++98 mode.Tajuanatak
I'm not going to dig up the history for you, but if the concept of value initialization was introduced as a defect report correcting the C++98 behavior, then compilers will retroactively implement that even for C++98 mode. In any case, why do distinctions between C++98 and C++03 even matter? Just accept the latter as what post-standardization C++ was until C++11.Nonparous
Most compilers make no distinction between C++03 and C++98. The former was effectively a bugfix for the latter, so compilers tend to merge them together and give you C++03 if you ask for C++98Holmberg
@vsoftco You mean the return type deduction? That's considered a DR against C++11.Tajuanatak
@Tajuanatak yes, I actually asked the question before #28955978Solenoid
Related: #27357212Tajuanatak
@Tajuanatak Wow, don't remember that at allPedicular
J
11

5.2.3 Explicit type conversion (functional notation)

2 The expression T(), where T is a simple-type-specifier (7.1.5.2) for a non-array complete object type or the (possibly cv-qualified) void type, creates an rvalue of the specified type, whose value is determined by default-initialization (8.5; no initialization is done for the void() case). [...]

8.5 Initializers

5 [...] To default-initialize an object of type T means:

-- if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

-- if T is an array type, each element is default-initialized;

-- otherwise, the storage for the object is zero-initialized.

There is no problem. int() has been guaranteed to evaluate to zero right from the very first C++ standard. The fact that it happened through default-initialization, rather than value-initialization, is a technical detail that is completely irrelevant for your question.

Jemima answered 2/6, 2015 at 18:20 Comment(7)
So, is it the task of default constructor to perform default initialization for built in types?Mennonite
@meet When it says "non-POD class type", that means "class type that is not a POD class type", not "type that is not a POD class type". The third item applies: the storage for the object is zero-initialized. No constructor gets used.Jemima
so exactly when constructor of built in type is called & used?Mennonite
@meet Built-in types don't have constructors. Constructors are member functions, and built-in types don't and can't have any members. So never. That said, some people do choose to say that int has a default constructor and that int() calls that default constructor. It's not how the C++ standard describes it, as you can see from my quote, and to my knowledge it's an uncommon point of view, but the end result is the same, so it's not wrong, merely possibly confusing.Jemima
But Bjarne Stroustrup in his book The C++ programming language says in section 10.4.2 that Built in types also have default constructorsMennonite
@meet Yes, he does say that, and that's the uncommon point of view that I was referring to. The C++ standard describes it differently, like I put here in my answer. (#5113865 is a relevant question about that.)Jemima
Let us continue this discussion in chat.Mennonite

© 2022 - 2024 — McMap. All rights reserved.