Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | memory
(LWG3870)
(Example: bit_cast of an initialised array produces an initialised object, add destroy_at call to uninitialise it)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{cpp/title|construct_at}}
 
{{cpp/title|construct_at}}
 
{{cpp/memory/navbar}}
 
{{cpp/memory/navbar}}
{{dcl begin}}
+
{{ddcl|header=memory|since=c++20|
{{dcl header|memory}}
+
{{dcl|since=c++20|
+
 
template< class T, class... Args >
 
template< class T, class... Args >
 
constexpr T* construct_at( T* p, Args&&... args );
 
constexpr T* construct_at( T* p, Args&&... args );
 
}}
 
}}
{{dcl end}}
 
  
 
Creates a {{tt|T}} object initialized with arguments {{c|args...}} at given address {{c|p}}. Specialization of this function template participates in overload resolution only if {{c|::new(std::declval<void*>()) T(std::declval<Args>()...)}} is well-formed in an unevaluated context.
 
Creates a {{tt|T}} object initialized with arguments {{c|args...}} at given address {{c|p}}. Specialization of this function template participates in overload resolution only if {{c|::new(std::declval<void*>()) T(std::declval<Args>()...)}} is well-formed in an unevaluated context.
  
 
Equivalent to
 
Equivalent to
{{source|return ::new (static_cast<void*>(p) T(std::forward<Args>(args)...);}}
+
{{source|return ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);}}
 
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]].
  
Line 27: Line 24:
  
 
===Example===
 
===Example===
{{example|code=
+
{{example
#include <iostream>
+
|code=
 +
#include <bit>
 
#include <memory>
 
#include <memory>
  
struct S
+
class S
 
{
 
{
     int x;
+
     int x_;
     float y;
+
     float y_;
     double z;
+
     double z_;
 
+
public:
     S(int x, float y, double z) : x{x}, y{y}, z{z} { std::cout << "S::S();\n"; }
+
     constexpr S(int x, float y, double z) : x_{x}, y_{y}, z_{z} {}
      
+
     [[nodiscard("no side-effects!")]]
    ~S() { std::cout << "S::~S();\n"; }
+
     constexpr bool operator==(const S&) const noexcept = default;
 
+
     void print() const
+
    {
+
        std::cout << "S { x=" << x << "; y=" << y << "; z=" << z << "; };\n";
+
    }
+
 
};
 
};
  
int main()
+
consteval bool test()
 
{
 
{
     alignas(S) unsigned char storage[sizeof(S)];
+
     alignas(S) unsigned char storage[sizeof(S)]{};
 
+
    S uninitialized = std::bit_cast<S>(storage);
     S* ptr = std::construct_at(reinterpret_cast<S*>(storage), 42, 2.71828f, 3.1415);
+
    std::destroy_at(&uninitialized);
     ptr->print();
+
     S* ptr = std::construct_at(std::addressof(uninitialized), 42, 2.71f, 3.14);
 
+
     const bool res{*ptr == S{42, 2.71f, 3.14}<!---->};
 
     std::destroy_at(ptr);
 
     std::destroy_at(ptr);
 +
    return res;
 
}
 
}
|output=
+
static_assert(test());
S::S();
+
 
S { x=42; y=2.71828; z=3.1415; };
+
int main() {}
S::~S();
+
 
}}
 
}}
  

Latest revision as of 03:58, 3 May 2024

 
 
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. Specialization of this function template participates in overload resolution only if ::new(std::declval<void*>()) T(std::declval<Args>()...) is well-formed in an unevaluated context.

Equivalent to

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

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

When construct_at is called in the evaluation of some constant expression e, the argument p must point to either storage obtained by std::allocator<T>::allocate or an object whose lifetime began within the evaluation of e.

Contents

[edit] Parameters

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

[edit] Return value

p

[edit] Example

#include <bit>
#include <memory>
 
class S
{
    int x_;
    float y_;
    double z_;
public:
    constexpr S(int x, float y, double z) : x_{x}, y_{y}, z_{z} {}
    [[nodiscard("no side-effects!")]]
    constexpr bool operator==(const S&) const noexcept = default;
};
 
consteval bool test()
{
    alignas(S) unsigned char storage[sizeof(S)]{};
    S uninitialized = std::bit_cast<S>(storage);
    std::destroy_at(&uninitialized);
    S* ptr = std::construct_at(std::addressof(uninitialized), 42, 2.71f, 3.14);
    const bool res{*ptr == S{42, 2.71f, 3.14}};
    std::destroy_at(ptr);
    return res;
}
static_assert(test());
 
int main() {}

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 3870 C++20 construct_at could create objects of a cv-qualified types only cv-unqualified types are permitted

[edit] See also

allocates uninitialized storage
(public member function of std::allocator<T>) [edit]
[static]
constructs an object in the allocated storage
(function template) [edit]
destroys an object at a given address
(function template) [edit]
creates an object at a given address
(niebloid)[edit]