Почему std::allocate_shared() разрешено использовать std::addressof() для объекта, который еще не создан?C++

Программы на C++. Форум разработчиков
Ответить
Гость
 Почему std::allocate_shared() разрешено использовать std::addressof() для объекта, который еще не создан?

Сообщение Гость »


Согласно примечаниям cppreference для

Код: Выделить всё

std::to_address()
,

Код: Выделить всё

std::addressof()
requires the object to be already constructed:

std::to_address can be used even when does not reference storage that has an object constructed in it, in which case

Код: Выделить всё

std::addressof(*p)
cannot be used because there is no valid object for the parameter of

Код: Выделить всё

std::addressof
to bind to.

But in this code in the standard library:

Код: Выделить всё

// https://github.com/llvm/llvm-project/blob/main/libcxx/include/__memory/shared_ptr.h
template  = 0>
_LIBCPP_HIDE_FROM_ABI shared_ptr allocate_shared(const _Alloc& __a, _Args&&... __args) {
using _ControlBlock          = __shared_ptr_emplace;
using _ControlBlockAllocator = typename __allocator_traits_rebind::type;
__allocation_guard __guard(__a, 1);
::new ((void*)std::addressof(*__guard.__get())) _ControlBlock(__a, std::forward(__args)...);
auto __control_block = __guard.__release_ptr();
return shared_ptr::__create_with_control_block(
(*__control_block).__get_elem(), std::addressof(*__control_block));
}
When using

Код: Выделить всё

placement new
,

Код: Выделить всё

__guard.__get()
does not reference storage that has an object constructed in it. Why can

Код: Выделить всё

std::addressof(*__guard.__get())
be used here? Or is my understanding wrong?


Источник: https://stackoverflow.com/questions/781 ... bject-that
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»