C++ Logo


Advanced search

Re: New way to declare smart pointers

From: Matthew Woehlke <mwoehlke.floss_at_[hidden]>
Date: Wed, 23 Oct 2019 13:28:08 -0400
On 23/10/2019 08.29, Aymeric Pellé via Std-Proposals wrote:
> This proposal aims to ease the basic use of smart pointers.
> The following declarations:
> T*s ptr;
> T*u ptr;
> T*w ptr;
> would be equivalent to this ones:
> std::shared_ptr<T> ptr;
> std::unique_ptr<T> ptr;
> std::weak_ptr<T> ptr;
> What do you think about it?

I think I would be fairly impressed if you can get a compiler to parse
these without ambiguity. I also think the parsing difficulty you would
impose on other tools is not worth it without some *serious*
justification. Saving keystrokes is not sufficient justification,
especially for a language-level change that creates such a strong
coupling to specific library types that, a few years from now, we might
decide were mistakes (remember auto_ptr?).

Anyway, nothing (aside from the horrible global namespace pollution)
stops you from writing:

  template <typename T> using u = std::unique_ptr<T>;
  template <typename T> using s = std::shared_ptr<T>;
  template <typename T> using w = std::weak_ptr<T>;

...and then writing:

  s<T> ptr;
  u<T> ptr;
  w<T> ptr;


Received on 2019-10-23 12:30:25