4 ms·
I've programmed in C++ for years and this behavior has never been a problem I've seen in any real codebase that I've worked on, and I've never heard anyone comp
by StellarScience 2y ago
I've programmed in C++ for years and this behavior has never been a problem I've seen in any real codebase that I've worked on, and I've never heard anyone complain about this behavior. And that's without ever seeing this behavior explained explicitly until today.
That's because the current behavior is intuitive to any programmer who understands the core design decisions of C++. Default parameters don't change the signature or name mangling of a function, and they have to be specified in the declaration rather than the implementation. So without being told, it's clear that compliers must handle them at the call site.
At the call site if you only know the object is of type Shape, the compiler can't possibly insert the default value for type Rectangle. Heck, in the general case Rectangle might be defined in a separate DLL or .so file that doesn't get loaded until run-time. So there's no possible way for the compiler to know its default value here. So without being taught, C++ programmers know what behavior to expect, and they're right.
That said, over time the use of default parameters has gone from encouraged to discouraged, largely to promote code clarity and explicitness. So it's a less important feature now than it was in 1997.
- cryptonector 2y ago> Heck, in the general case Rectangle might be defined in a separate DLL or .so file that doesn't get loaded until run-time. So there's no possible way for the compiler to know its default value here. Eh, that's not exactly true. The ABI for C++ could have included a way to look up the default values of defaulted parameters from the object's run-time class. But the C++ ABI didn't need that. Oh, and there's no C++ ABI, but this behavior would have been part of the de facto sort-of ABIs if it had been required by the spec (but it wasn't). That said, I agree with you that this is intuitive.