6 comments

  • quietbritishjim30 minutes ago
    I followed the link near the start to the article about std::indirect&lt;T&gt;, which you can use instead of std::unique_ptr&lt;T&gt; for the pImpl idiom: [1]<p>&gt; 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)<p>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&#x27;t possible here because you&#x27;d either have to re-init your moved-from object, or do element-wise move (with `MyClass(MyClass&amp;&amp; other): impl(std::in_place, *other.impl) {}`) but those would stop your move constructor being noexcept. It would&#x27;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.<p>[1] <a href="https:&#x2F;&#x2F;mariusbancila.ro&#x2F;blog&#x2F;2026&#x2F;07&#x2F;23&#x2F;the-pimpl-idiom-and-the-cpp26-stdindirect-type&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mariusbancila.ro&#x2F;blog&#x2F;2026&#x2F;07&#x2F;23&#x2F;the-pimpl-idiom-and...</a>
  • usrnm1 hour ago
    15 years ago I would&#x27;ve been happy reading about all these cool new things coming to C++, nowadays I&#x27;m happy I don&#x27;t use it anymore and don&#x27;t need to relearn everything again. Am I getting old?
  • nananana944 minutes ago
    We&#x27;ve been doing this for 50 years, you don&#x27;t need a class hierarchy for your event struct.<p><pre><code> 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&lt;Event&gt; events;</code></pre>
    • quietbritishjim40 minutes ago
      That doesn&#x27;t work if you are writing a library where users of the library are able to implement their own event types.<p>Even for a self-contained code base, it&#x27;s a pain compared to a type that just works with any current or future derived class.
      • nananana930 minutes ago
        It does.<p><pre><code> enum EventType { ..., EVENT_USER_START = 0xF0000000, }; struct UserEventData { uint8 placeholder[64]; } struct Event { EventType type; union { UserEventData user; }; } </code></pre> That aside, have you ever felt the need to implement your own Win32 event, or SDL event, or X11 event, or epoll_event, or a custom DOM event or whatever?<p>If you&#x27;re writing a good library, give the user the data they need, and let them handle it however they wish, don&#x27;t force them int your framework.
    • adev_29 minutes ago
      What you wrote there is just an unsafe way to do std::variant. This has nothing to do with what the blog post is about.<p>The blog is about <i>Open</i> polymorphism. Where the list of compatible Object can be extended dynamically (Which is handy for some type of error report for instance).<p>You can do that since forever in C++ (Since the first support for inheritance), the only thing std::polymorphism brings is the ability to do it without dynamic allocations, emplacement new or lifetime problems.
      • nananana926 minutes ago
        &gt; This has nothing to do with what the blog post is about.<p>It&#x27;s an alternative way to solve the problem presented, which trivially allows you to make a std::vector&lt;Event&gt; (the whole point the article is building up to).<p>&gt; the only thing std::polymorphism brings is the ability to do it without dynamic allocations, emplacement new or lifetime problems.<p>Which the version I showed has none of.
        • adev_22 minutes ago
          &gt; it&#x27;s an alternative way to solve the problem presented<p>No it&#x27;s not and you apparently did not understand the concept behind it (or more likely, do not want to understand the concept behind it).<p>Your solution is not an implementation of Open Polymorphism, it is just a poor man implementation of std::variant.<p>&gt; Which the version I showed has none of.<p>Because it is not the same thing like explain by my previous comment and in around 5 other comments around mine soon I guess.<p>But you are entirely free to grunt around and continue to imply that C++ committee people are implementing something useless (Which they are not, this is useful).
          • nananana911 minutes ago
            My point is that you don&#x27;t need open polymorphism for the example given, and if you give me another example, I can show you why you don&#x27;t need it there either -- because you do in fact never need it. It&#x27;s an XY problem over the set X.<p>If a programmer is reaching for a library, they want it to solve a concrete task. Nobody wants to subclass your Event type, if I have to do it, you&#x27;ve failed at your job.
    • fooker22 minutes ago
      When this piece of code exists in a library, it makes it pretty much impossible to extend by the user of the library.
    • Panzerschrek33 minutes ago
      Typical C approach. In C++ this should be done with <i>std::variant</i> instead. It has the exact same binary layout, but is type-safe (compared to union usage).
  • Panzerschrek35 minutes ago
    Does <i>std::polymorphic</i> require passed types to be copyable?
    • aw162110724 minutes ago
      Looks like it depends on the specific operation(s) you want to perform [0]. For instance, constructing a std::polymorphic&lt;T&gt; from an object of type T or derived from T requires that that object is copy-constructible (among other requirements), but copy-assigning one std::polymorphic&lt;T&gt; to another only requires that T is complete.<p>[0]: <a href="https:&#x2F;&#x2F;eel.is&#x2F;c++draft&#x2F;polymorphic" rel="nofollow">https:&#x2F;&#x2F;eel.is&#x2F;c++draft&#x2F;polymorphic</a>
  • injidup1 minute ago
    [dead]
  • ho_schi48 minutes ago
    I need <i>default security</i> with a <i>#secure</i> and <i>#insescure</i>.<p>Not adding more stuff. So I get security, legacy compatibility and the existing flexibility.