Giter Site home page Giter Site logo

nn's Introduction

nn: Non-nullable pointers for C++

nn is a type that helps you enforce at compile time the contract that a given pointer can't be null. It wraps around raw pointers or smart pointers, and works particularly well out of the box with std::unique_ptr and std::shared_ptr.

Here's an example:

class Widget : public WidgetBase {
public:
  Widget(nn_shared_ptr<Gadget> gadget) : m_gadget(move(gadget)) {}
  // ...
private:
  nn_shared_ptr<Gadget> m_gadget;
};

// nn_make_unique and nn_make_shared always return non-null values
nn_unique_ptr<Widget> my_widget = nn_make_unique<Widget>(nn_make_shared<Gadget>());

// but what if we have a pointer already and we don't know if it's null?
shared_ptr<Gadget> this_might_be_null = ...
my_widget = nn_make_unique<Widget>(NN_CHECK_ASSERT(this_might_be_null));

// implicit nn_unique_ptr -> nn_shared_ptr works just like unique_ptr -> shared_ptr
nn_shared_ptr<Widget> shared_widget = move(my_widget);

// the `nn` implicitly casts away if needed
void save_ownership_somewhere(shared_ptr<Widget>);
save_ownership_somewhere(shared_widget);

// implicit upcasts work too
nn_shared_ptr<WidgetBase> base_ptr = shared_widget;

Compile-time checking helps find bugs sooner. At Dropbox we use nn pervasively in our cross-platform C++ codebase.

Compatibility

nn is C++11 compatible. Tested with GCC 4.8, clang 3.6+ and MSVS 2015.

nn's People

Contributors

artwyman avatar j4cbo avatar nlohmann avatar webmaster128 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.