Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I explicitly zero initialize auto_ptr?

Tags:

c++

auto-ptr

Some of my colleagues prefer to explicitly initialize std::auto_ptr to 0 in constructor initialization list, but it will be initialized to 0 in it's constructor without any explicit initialization. So is there any reason to do it?

#include <memory>

class A
{
  A() : SomePtr(0)
  {
  }

private:
  std::auto_ptr<SomeType> SomePtr;
};
like image 436
ks1322 Avatar asked Jan 19 '23 13:01

ks1322


1 Answers

No, the default constructor of std::auto_ptr does exactly that, so doing it explicitly is not necessary. In any case, it's a matter of style and you should be consistent. For instance, would you explicitly call the default constructor of a member vector in the constructor initialization list?

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

like image 136
Armen Tsirunyan Avatar answered Jan 29 '23 06:01

Armen Tsirunyan