Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specific use case of to_address

So apparently C++20 is getting std::to_address.

From the cppreference page its use case doesn't seem clear to me. We already have operator& and std::addressof, why do we need yet another function that gives us an address to its argument?

like image 499
Hatted Rooster Avatar asked Jun 07 '19 11:06

Hatted Rooster


1 Answers

std::addressof takes an object and gets its address, even if unary "addressof operator" (aka &) was overloaded.

std::to_address takes a pointer, smart or dumb, and returns a pointer.

Basically when writing the std library, in this case allocators, implementors find they needed this utility function. It is small, simple, and has to be written whenever someone wants to work with allocators. So they wrote a no-brainer proposal to add it.

There are some traps here; you cannot do std::addressof(*ptr) because *ptr isn't always an object yet. There is already a trait that solves this, but writing code using traits directly is annoying.


Why this when they haven't finished your favourite feature? Like networking?

In comparison, networking is not a no-brainer proposal. And the current design depends on executors (basically abstractions of the concept of thread pools). The goal of writing a high level library that offers hand crafted C/ASM performance makes writing networking harder than a 2 line utility function.

Then somebody complains that they take 15 minutes to approve a no-brainer utility function, because the multiple programmer year epic proposal isn't in yet. The injustice.

Or something like that.

like image 160
Yakk - Adam Nevraumont Avatar answered Sep 21 '22 05:09

Yakk - Adam Nevraumont