• sonymegadrive@feddit.uk
    link
    fedilink
    arrow-up
    16
    ·
    4 months ago

    C++: Nuh, uh …

    template <typename T>
    concept Crackable = requires(T obj) {
        { obj.crack() };
    };
    
    auto crack(Crackable auto& nut) {
        nut.crack();
    }
    
    • Aatube@kbin.melroy.org
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      4 months ago

      This is dangerous. The object might not have the crack() method, and this bloats the compiled size by a lot if you use it with different types. There’s also no reason I can see to use concepts here. The saner way would probably be to use inheritance and objects to mimic Java interfaces.