Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When actually primitive type constructor gets called & used? [duplicate]

After reading this article I made a point that int () yields 0 because the temporary int is value initialized and not because int() calls the default constructor for int. (The article is flawed according to my understanding.)

I also said that primitive (built-in) types don't have constructors. The original author asked me to check Section $10.4.2 (TC++PL) which says

Built-in types also have default constructors ($6.2.8)

But I still think that the statement "C++ allows even built-in type (primitive types) to have default constructors." is flawed (as per C++03).

I think Bjarne in TC++PL has mixed up "constructor like notation i.e ()" with actual constructor call. Value initialization was not introduced at that time when Bjarne was writing the book, right? So is the text in TC++PL incorrect as per C++98 and C++03?

What do you guys think?

EDIT

I asked Bjarne personally (via mail) regarding the flawed text in TC++PL and this was his reply

I think you mix up "actual constructor calls" with conceptually having a constructor. Built-in types are considered to have constructors (whatever words the standard uses to describe their behavior).

like image 405
Prasoon Saurav Avatar asked Feb 25 '11 03:02

Prasoon Saurav


2 Answers

Simple Answer: Technically No.

Long Answer:

No. But!

The syntax you use to initialize them makes them look like they are being constructed by a default constructor or a default copy constructor.

int x0(5);     // Looks like a constructor. Behaves like one: x is initialized.
int x1{5};

int y0();      // Fail. Actually a function declaration.
// BUT
int y1{};      // So new syntax to allow for zero initialization
int z0 = int();// Looks like a constructor. Behaves like a constructor (0 init).
int z1 = int{};

int a0(b);     // Again.
int a1{b};

So technically there are no constructors for basic-POD types. But for all intents and purposes they act just like they have a copy constructor and default constructor (when initialized with the braces).

If it looks like a duck and quacks like a duck, then its very duck like.

like image 145
Martin York Avatar answered Oct 07 '22 22:10

Martin York


A constructor is a member function (constructors are fully specified in clause 12 of the C++ Standard, which covers special member functions like constructors and destructors).

A member function can only be defined for a class type (C++03 9.3/1 says "Functions declared in the definition of a class, excluding those declared with a friend specifier, are called member functions of that class").

So non-class types (including fundamental types, array types, reference types, pointer types, and enum types) do not have constructors.

I don't have a copy of The C++ Programming Language to read the context of the quote that "Built-in types also have default constructors," but I would guess that Stroustrup is either using the term "constructor" in a loose, non-technical sense, or the meaning of the term or the way in which it is used in the Standard changed between when the book was published and when the language was standardized. I'd guess the former is far more likely than the latter.

like image 33
James McNellis Avatar answered Oct 07 '22 23:10

James McNellis