Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


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.


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


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.


But... all of the classes in the article -do- have default constructors. And all of the examples in the article do compile. So I'm confused at what point you guys are making.


Quoting the full section on `T t;`:

- If T is a class type and there is a default constructor, run it.

- If T is an array type, default-initialize each element.

- Otherwise, do nothing.

That decision tree should read: "If T is a class type: it will invoke the default constructor. It is a compile-time error to write this if T does not have a default constructor." Not "if there is a default constructor, run it; otherwise, fall back to doing nothing."

The "do nothing" applies to scalar types such as ints, and indirectly to scalar member variables that aren't explicitly initialized one way or another. Not to classes that have deleted the default constructor.


Yes, this. It appears these commenters are claiming that the author said something they did not say. Rather, there is a corner case that the author has not spoken about in this article.

... Which is not surprising, given that we're talking about a language with a spec that dwarfs most works of human literature for sheer mass.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: