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++?

Destructor
  • 14,123
  • 11
  • 61
  • 126
  • 4
    Please quote the relevant text from the book. – chris Jul 21 '14 at 14:48
  • 1
    Indeed, without that we cannot clarify the situation. Although, reading that linked text, **POD != primitive**. – aruisdante Jul 21 '14 at 14:49
  • 1
    You can pretend they have constructors for most syntax. – Neil Kirk Jul 21 '14 at 14:50
  • 3
    You can safely assume Mr. Stroustrup is more reliable about C++ than a random website. – Isaac Jul 21 '14 at 14:51
  • By the way, a POD class type must have a trivial default constructor per §9 [class]/6 and /10. – chris Jul 21 '14 at 14:51
  • 2
    @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](https://isocpp.org/wiki/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. – chris Jul 21 '14 at 14:52
  • @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. – Isaac Jul 21 '14 at 14:58
  • @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. – Isaac Jul 21 '14 at 15:03
  • Similar question: http://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors – chris Jul 21 '14 at 15:05
  • @Isaac well, they do have them, they just must be automatically generated by the compiler. – aruisdante Jul 21 '14 at 15:05
  • @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. – chris Jul 21 '14 at 15:05

1 Answers1

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.

gexicide
  • 38,535
  • 21
  • 92
  • 152
  • 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. – aruisdante Jul 21 '14 at 14:56
  • 3
    @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).* – chris Jul 21 '14 at 14:58
  • @chris 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. – aruisdante Jul 21 '14 at 15:03