← Back to context

Comment by Too

10 years ago

Following this rule, maybe it's time to add a shorthand for std::unique_ptr<> to c++ already. I'm not really a fan of asterixes or ampersands either but 17 characters, 5 of them requiring shift modifier, for something you write that often quickly becomes annoying.

Usually I define new types, when I use them a lot like:

    class Something {
      typedef std::unique_ptr<Something> unique;
      typedef std::shared_ptr<Something> shared;
      typedef std::weak_ptr<Something> weak;
        
    };

    // ...
    // later in the code
    
    Something::unique a;
    // instead of std::unique_ptr<Something> a;

  • That's a neat way of handling it. I wouldn't have considered it. I think I'm gonna steal it.

template<class T> using unq = std::unique_ptr<T>;

This is a start, then I usually define specific types from there.