Skip to content

Comment on The missing C++ smart pointer

Comments

I'd rather have an optionally-owned pointer type so I can handle writing virtual methods that can return a value aliasing an already existing value or create a new one on demand. Otherwise you either have to roll your own or bloat your API:

  virtual std::flexptr<Thing> get_expensive_thing();
vs:
  virtual bool has_expensive_thing();
  virtual const Thing& get_expensive_thing();
  virtual std::unique_ptr<Thing> build_expensive_thing();
It'd probably have shared_ptr semantics but you'd have to treat it as a const ref for lifetime purposes, which might make it distasteful to the std library folks.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.