Do primitive types have also constructors in C++?
Asked Answered
N

1

6

I have read in Dr. Bjarne Stroustrup Book "The C++ Programming Language" 3rd edition that built in types have also constructors in C++ in section 10.4.2.

But then the following link says that POD types can't have constructors:

http://www.parashift.com/c++-faq-lite/pod-types.html

Which is true? Do primitive types have also constructors in C++?

Nappy answered 21/7, 2014 at 14:47 Comment(11)
Please quote the relevant text from the book.Neuron
Indeed, without that we cannot clarify the situation. Although, reading that linked text, POD != primitive.Redbud
You can pretend they have constructors for most syntax.Bronchopneumonia
You can safely assume Mr. Stroustrup is more reliable about C++ than a random website.Hokkaido
By the way, a POD class type must have a trivial default constructor per §9 [class]/6 and /10.Neuron
@Isaac, That FAQ is a recognized C++ resource. In fact, it and Bjarne's own FAQ were mostly combined to make the official C++ FAQ (or the closest to official we can have). Anyway, the FAQ should really say no user-defined constructors, but when it was written, there was no way to define a trivial constructor, so I guess it should say that it needs the implicit compiler-generated constructors.Neuron
@chris: Interesting. So it's not just a random website. I guess custom constructors is what this FAQ actually meant. The primitive types already have a constructor.Hokkaido
@aruisdante: OK. The link is about POD types and it says they cannot have a constructor hence they are usable in C too. The OP is asking whether primitive types have constructor or not. The bottom-line is the primitive types in C++ does have constructor, but PODs don't have.Hokkaido
Similar question: #5113865Neuron
@Hokkaido well, they do have them, they just must be automatically generated by the compiler.Redbud
@Isaac, POD types (including the built-in types) are not required to have one, but POD-class types are, but not a user-defined one.Neuron
S
8

What Bjarne means is that you can write int(56) or even int() to construct an integer. What the links means is that a struct/class is only a POD if it does not have a constructor declared. So Bjarne talks about primitive non-struct types and the link talks about structs/classes so the two sources can coexist without contradicting each other.

Here is part of the definition from the link:

a POD type's non-static data members must be public and can be of any of these types

Of course, this can only hold for structs. An int has no "data members". So although the link never mentions it directly, it only refers to structs and classes.

Sulphurize answered 21/7, 2014 at 14:50 Comment(3)
A POD also must have no methods (not just no declared constructor/destructor). Basically, you can only do what is allowed with a C struct if you want the struct to be portable between C++ and C without modification.Redbud
@aruisdante, Not true. Only virtual functions are prohibited. FWIW, I'm talking about C++11, but the C++03 text is A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor, where An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).Neuron
@Neuron You're right, I had read it as making a struct that would compile with both, but it just meas a struct that acts like a C-struct for initialization/copy, not that would be compilable as such.Redbud

© 2022 - 2024 — McMap. All rights reserved.