logoalt Hacker News

Composite objects and C++26 the std:polymorphic type

33 points • by jandeboevrie • last Monday at 5:54 AM • 29 comments • view on HN

Comments

quietbritishjim • today at 7:57 AM

I followed the link near the start to the article about std::indirect<T>, which you can use instead of std::unique_ptr<T> for the pImpl idiom: [1]

> Never null: it always holds a value, except in the moved-from state (but features a member function called valueless_after_move() that lets you check the state)

Ugh, what a mess. Surely the whole point of using this rather than unique_ptr is to avoid such a trap! A safe move constructor isn't possible here because you'd either have to re-init your moved-from object, or do element-wise move (with `MyClass(MyClass&& other): impl(std::in_place, *other.impl) {}`) but those would stop your move constructor being noexcept. It would've been much more useful to make the move constructor of indirect be deleted, which is what people typically did with a pImpl-based classes in the past.

[1] https://mariusbancila.ro/blog/2026/07/23/the-pimpl-idiom-and...

➕ show 1 reply
nananana9 • today at 7:42 AM

We've been doing this for 50 years, you don't need a class hierarchy for your event struct.

  enum EventType {
      EVENT_NONE,
      EVENT_TEMPERATURE_READING,
      EVENT_SMOKE_DETECTED,
      EVENT_CARBON_MONOXIDE_DETECTED,
  };
  struct Event {
      EventType type;
      union {
          TemperatureDeadingData temperature_reading;
          SmokeDetectedData smoke_detected;
          CarbonMonoxideDetected carbon_monoxide_detected
      } data;
  };

  std::vector<Event> events;
➕ show 7 replies
usrnm • today at 7:15 AM

15 years ago I would've been happy reading about all these cool new things coming to C++, nowadays I'm happy I don't use it anymore and don't need to relearn everything again. Am I getting old?

➕ show 3 replies
Panzerschrek • today at 7:52 AM

Does std::polymorphic require passed types to be copyable?

➕ show 2 replies
injidup • today at 8:26 AM

[dead]

ho_schi • today at 7:38 AM

I need default security with a #secure and #insescure.

Not adding more stuff. So I get security, legacy compatibility and the existing flexibility.