9

ISO/IEC 14882:2020
22.2.1.16 Note 8: The default construct in allocator will call ::new ((void*)p) T(args), but specialized allocators can choose a different definition.

Would you be so kind as to tell me whether there's any reason it's not ::new (static_cast<void*>(p)) T(args)?

Ernie Sanderson
  • 382
  • 2
  • 10
  • 1
    The static_cast would fail for const types -- the C style cast is equivalent to const_cast(static_cast(p)) – Chris Dodd Dec 28 '21 at 08:56
  • @ChrisDodd, but if `p` is `const` we would be in trouble in the first place, it would be UB. no? a pure `static_cast` would catch that. – alfC Dec 28 '21 at 09:05
  • `const` objects can still be initialized, it just needs to be possible to do it at compiler time. – Chris Dodd Dec 28 '21 at 09:11
  • 1
    @alfC: anyway const pointer can point to non-const object. and it is modifying const object which is UB. – Jarod42 Dec 28 '21 at 09:26
  • @Jarod42 I understand that, but there is no way to know that locally. I found this line to be inconsistent across the cppreference documentation, for example, here (the more correct IMO) `static_cast` is used: https://en.cppreference.com/w/cpp/memory/allocator_traits/construct – alfC Dec 28 '21 at 09:36
  • The cast as written is legal and its meaning is well-defined. Using the long-winded version would obscure the point of the note. – Pete Becker Dec 28 '21 at 14:10

0 Answers0