Is this a Nut?

Sjmarf@sh.itjust.works to Programmer Humor@programming.dev – 1272 points –
129

You are viewing a single comment

C++: Nuh, uh ...

template 
concept Crackable = requires(T obj) {
    { obj.crack() };
};

auto crack(Crackable auto& nut) {
    nut.crack();
}

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.