On Tue, Nov 26, 2019 at 4:03 AM Michael Hava via Std-Proposals <std-proposals@lists.isocpp.org> wrote:

(Honestly, this makes me wonder why nullptr can construct an empty std::function, but it’s probably best not to go down that rabbit hole.)

 

I’m guessing that constructor is for symmetry with function pointers (https://godbolt.org/z/UftWpU).


Yeah, std::function has a lot of historical cruft around function pointer types.

The upside of this facility is that it makes std::function<void()> a little more of a drop-in replacement for void (*)() — if you put nullptr in, you get something falsey out. The downside is that it causes extra codegen for a situation (storing null function pointers) that in most codebases will never actually arise. You’re paying for something you don’t intend to use.

Here's a more direct example of how the nullptr_t constructor helps "symmetry with function pointers." https://godbolt.org/z/Nk6Nhe

–Arthur