top | item 40885337

(no title)

pierrebai | 1 year ago

The article is just plain wrong about classes: if you have declared any constructor, then the language will not provide a default constructor and default-initialization will fail with a compiler diagnostic.

So their claim that "T t;" will "do nothing" is incorrect.

    class T
    {
    public:
        T(int);
    };
    T t;
Will fail.

discuss

order

wavemode|1 year ago

I'm confused at how your comment is relevant to this article. Here you've written `T(int);`, i.e. a constructor with an argument. None of the classes declared in the article have a constructor that takes any arguments. Nor does the text of the article seem to make any statement which contradicts what you've asserted here. And all of the examples in the article compile successfully.

shadowgovt|1 year ago

I think I missed where in the article they did a `T t;`... Doesn't seem to show up with an eyeball scan?

vitus|1 year ago

In the first few paragraphs:

> Primarily, there are two kinds of initialization of concern: default-initialization and value-initialization. The rules given in the standard look roughly like this:

> * For any type T, T t; performs default-initialization on t as follows: ...

As GP mentions, the article's descriptions of default and value initialization are both incorrect for classes that do not have default constructors, as that code will simply not compile.