Should I explicitly initialize auto_ptr?

Some of my colleagues prefer to explicitly initialize std::auto_ptr to 0 in the constructor initialization list, but it will be initialized to 0 in it by the constructor without any explicit initialization. So is there a reason for this?

 #include <memory> class A { A() : SomePtr(0) { } private: std::auto_ptr<SomeType> SomePtr; }; 
+4
source share
3 answers

No, the default constructor std::auto_ptr does just that, so obviously this is not necessary. In any case, it is a matter of style, and you must be consistent. For example, would you explicitly call the default constructor for the member vector in the constructor initialization list?

As a side note, std::auto_ptr is deprecated in the following standard

+9
source

Psychology.

For built-in types, you probably already know that they are not initialized unless you explicitly do so. For classes, this is not so.

The desire for consistency leads to explicit initialization everywhere. This allows you to forget if A::SomePtr is an inline or class type. Pretty useless, imho, since the number of built-in types is quite limited.

0
source

One reason may be clarity, but it should be the only one. I myself prefer not to write unnecessary utilization, especially if it completely saves me from writing a default constructor for the surrounding class and just let the compiler do its job. While this is just a matter of style, I think that too much paranoia even harms the clarity of the code.

0
source

All Articles