C++ Logo

std-proposals

Advanced search

Re: Adding a nullptr_t constructor to weak_ptr

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Tue, 26 Nov 2019 09:36:03 -0500
On Tue, Nov 26, 2019 at 4:03 AM Michael Hava via Std-Proposals <
std-proposals_at_[hidden]> 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.
See
https://quuxplusone.github.io/blog/2019/03/27/design-space-for-std-function/#on-line-b-can-we-be-confident-th
 —

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

Received on 2019-11-26 08:39:10