• @[email protected]
    link
    fedilink
    168 months ago

    C++: Nuh, uh …

    template 
    concept Crackable = requires(T obj) {
        { obj.crack() };
    };
    
    auto crack(Crackable auto& nut) {
        nut.crack();
    }
    
    • Aatube
      link
      fedilink
      2
      edit-2
      8 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.