By Sombrero Chicken

2019-06-07 11:44:13 8 Comments

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?


@Yakk - Adam Nevraumont 2019-06-07 12:18:11

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.

@L. F. 2019-06-07 12:19:40

It's really smart to call plain pointers dumb ... well, isn't it?

@Sombrero Chicken 2019-06-07 12:35:25

Fair enough, I just don't understand why they need to add all these utilities that library writers want to use to std. Just add it to std::we_are_cool_lib_writers or something.

@Caleth 2019-06-07 13:09:15

@SombreroChicken The flip side is that if you don't specify that all these boring, implementation detail-ly, utility functions that are necessary to express std::interesting_thing as portable C++, people wanting to write interesting_thing_with_important_difference complain that they can't do what std does. E.g. std::vector needs some magic to create an array from a bunch of adjacent objects, and that can't be replicated in user code.

Related Questions

Sponsored Content

28 Answered Questions

[SOLVED] enum to string in modern C++11 / C++14 / C++17 and future C++20

24 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

  • 2014-03-03 11:54:16
  • gEdringer
  • 284667 View
  • 1470 Score
  • 21 Answer
  • Tags:   c++ pointers c++11

1 Answered Questions

[SOLVED] Why std::overload instead of concepts/constexpr if in std::visit?

6 Answered Questions

[SOLVED] Function passed as template argument

9 Answered Questions

[SOLVED] How do I execute a command and get output of command within C++ using POSIX?

  • 2009-01-26 05:11:03
  • Misha M
  • 388505 View
  • 397 Score
  • 9 Answer
  • Tags:   c++ process posix

10 Answered Questions

5 Answered Questions

[SOLVED] Why do we require requires requires?

8 Answered Questions

[SOLVED] When should I really use noexcept?

19 Answered Questions

[SOLVED] int a[] = {1,2,}; Weird comma allowed. Any particular reason?

Sponsored Content