Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trivial raw pointer that self-initializes to nullptr in C++

I like the new pointer types in C++11, but sometimes I still need a raw pointer. Something that makes me increasingly sad about "raw" types in C++, however, is their habit of initializing as undefined when not given an explicit value. As I use std::shared_ptr<> and the like more often, this need to initialize raw pointers to null feels increasingly brittle and unnecessary. I'm talking about:

class foo
{
    ...
    std::shared_ptr< bar > pb;   // Initially null in whatever constructor.
    std::unique_ptr< dar > pd;   // Likewise.
    std::weak_ptr< gar > pg;     // And again.
    lar* pr;                     // Uh-oh! Who knows what this is? Better remember to initialize...
};

foo::foo( int j )
: pr( nullptr )
{...}

foo::foo( const string& s )
: pr( nullptr )
{...}

... etc.: many tedious and error-prone constructor definitions follow.

What I'd like, therefore, is a "raw pointer with null initialization." Something like:

class foo
{
    ...
    std::shared_ptr< bar > pb;   // Initially null in whatever constructor.
    std::unique_ptr< dar > pd;   // Likewise.
    std::weak_ptr< gar > pg;     // And again.
    raw_ptr< lar > pr;           // Once more with feeling.
};

foo::foo( int j )
{...}                            // No explicit pointer initialization necessary.

foo::foo( const string& s )
{...}

...

More precisely, what I want is a simple, cheap type that acts exactly like a raw pointer in every way except that its default constructor initializes it to nullptr.

My question: (1) Does such a thing already exist in the standard library? (2) If not, what would be the most elegant/smallest way to accomplish an implementation of this type?

P.S. I'm not interested in Boost or any other libraries, unless perhaps it is a header-only library in a single file. Smallness and simplicity are of the essence.

like image 763
OldPeculier Avatar asked Nov 28 '22 11:11

OldPeculier


2 Answers

C++11 allows in class initialization of data members.

class foo
{
  // ...
  lar *pr = nullptr;
};

That'll always initialize pr to nullptr unless you assign another value in the constructor.

like image 101
Praetorian Avatar answered Dec 05 '22 16:12

Praetorian


It looks like you need "World’s Dumbest Smart Pointer" which has been proposed as an addition to a future C++ standard template library. You can see the proposal here: "A Proposal for the World’s Dumbest Smart Pointer" and here: "A Proposal for the World’s Dumbest Smart Pointer, v2" and here: "A Proposal for the World’s Dumbest Smart Pointer, v3"

The proposal contains a potential partial implementation which you may be able to adapt for use with the compiler you are currently using. The implementation is similar to the almost_raw_ptr solution provided by Mark Ransom. A web search for exempt_ptr will give more details.

A proposal for the World’s Dumbest Smart Pointer, v3 has "Renamed exempt_ptr to observer_ptr" see the linked doument for other changes.

like image 43
ChetS Avatar answered Dec 05 '22 16:12

ChetS