[cfe-dev] Default arguments considered harmful?

David Chisnall David.Chisnall at cl.cam.ac.uk
Fri May 2 02:18:09 PDT 2014


On 2 May 2014, at 09:16, David Tweed <david.tweed at arm.com> wrote:

> I wonder if, with C++11 named initialization syntax for PODs, something
> could be done so that in cases like this a (function specific, I guess)
> "optional options object" could be used. (This would avoid one of the big
> problems with C++ optional arguments, which is that if one towards the end
> needs to be set to a non-default value all the preceding options need
> setting.) That would certainly make things a lot easier to read in cases
> like these.

The related issue is that they complicate overloading.  I've been bitten a few times in clang code by the compiler deciding that, when I specified a pointer instead of a reference for a parameter, it would happily coerce it to a bool and then use the default values for the rest of the parameters, giving some very strange results - no errors, no warnings, just the code doing something unexpected.

David





More information about the cfe-dev mailing list