Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/construct at"

From cppreference.com
< cpp‎ | memory
m (Notes)
m (relocate)
Line 16: Line 16:
 
}}
 
}}
 
except that {{tt|construct_at}} may be used in evaluation of [[cpp/language/constant expression|constant expressions]].
 
except that {{tt|construct_at}} may be used in evaluation of [[cpp/language/constant expression|constant expressions]].
 +
 +
While called in the evaluation of some expression {{c|e}}, {{tt|construct_at}} prevents {{c|e}} from being a constant expression if {{tt|p}} points to neither storaged obtained by {{c|std::allocator<T>::allocate}} nor an object whose lifetime began within the evaluation of {{c|e}}, or underlying initialization does not meet the requirements of constant evaluation.
  
 
===Parameters===
 
===Parameters===
Line 25: Line 27:
 
===Return value===
 
===Return value===
 
{{tt|p}}
 
{{tt|p}}
 
===Notes===
 
While called in the evaluation of some expression {{c|e}}, {{tt|construct_at}} prevents {{c|e}} from being a constant expression if {{tt|p}} points to neither storaged obtained by {{c|std::allocator<T>::allocate}} nor an object whose lifetime began within the evaluation of {{c|e}}, or underlying initialization does not meet the requirements of constant evaluation.
 
  
 
===Example===
 
===Example===

Revision as of 22:26, 6 February 2020

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
Defined in header <memory>
template<class T, class... Args>
constexpr T* construct_at( T* p, Args&&... args );
(since C++20)

Creates a T object initialized with arguments args... at given address p. This overload participates in overload resolution only if ::new(std::declval<void*>()) T(std::declval<Args>()...) is well-formed in evaluated context.

Equivalent to

return ::new (const_cast<void*>(static_cast<const volatile void*>(p)))
    T(std::forward<Args>(args)...);

except that construct_at may be used in evaluation of constant expressions.

While called in the evaluation of some expression e, construct_at prevents e from being a constant expression if p points to neither storaged obtained by std::allocator<T>::allocate nor an object whose lifetime began within the evaluation of e, or underlying initialization does not meet the requirements of constant evaluation.

Contents

Parameters

p - pointer to the uninitialized storage on which a T object will be constructed
args... - arguments used for initialization

Return value

p

Example

See also

[static]
constructs an object in the allocated storage
(function template) [edit]
destroys an object at a given address
(function template) [edit]