# pnew

Simplifies the description of placement new (placement new).

```cpp
template <class T, class... Args>
T* pnew(T& obj, Args&&... args) {
    return (T*)new ((void*)&obj) T(std::forward<Args&&>(args)...);
}
```

For example, use the following Constructor arguments can also be given.

```cpp
class my_class {
    int _a;
public:
    my_class(int a = -1) : _a(a) {}
};

my_class obj_1; // This constructor will not be called!
my_class obj_2; // This constructor will not be called!

void setup() {
    mwx::pnew(obj_1);    // my_class obj_1;  
	mwx::pnew(obj_2, 2); // my_class obj_2(2);
    ...
}
```

## background

Since the constructor of the global object is not called due to compiler constraints, one way to initialize it is to use placement new. However, since the syntax of placement new (placement new) appears to be complicated.

Another method is to use `std::unique_ptr` (or `eastl::unique_ptr`).

```cpp
std::unique_ptr<my_class> obj_3;

void setup() {
    obj_3.reset(new my_class(3));
    		//  Since the TWELITE microcontroller only allocates new once and cannot delete It is effectively equivalent to a global object.
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mwx.twelite.info/latest_en/api-reference/funcs/utility/pnew.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
