placement-new

Using placement new in a container

旧街凉风 提交于 2021-02-11 05:03:38
问题 I just came across some container implementation in C++. That class uses an internal buffer to manage its objects. This is a simplified version without safety checks : template <typename E> class Container { public: Container() : buffer(new E[100]), size(0) {} ~Container() { delete [] buffer; } void Add() { buffer[size] = E(); size++; } void Remove() { size--; buffer[size].~E(); } private: E* buffer; int size; }; AFAIK this will construct/destruct E objects redundantly in Container() and

Mixing pass-by-reference and pass-by-value to variadic template function valid?

此生再无相见时 提交于 2021-02-08 14:11:27
问题 I have a method which allocates memory for a object and then calls its constructor - a memory allocator. template <class T, typename... Arguments> inline T* AllocateObject(Arguments... args) { return new (InternalAllocate(sizeof(T))) T(args...); } Is it valid using this function to mix pass-by-value and pass-by-reference? For example allocating a class with a constructor with some by-value and some by-reference. It compiles, but I'm not sure if it has any nasty side-effects or not. 回答1: What

Mixing pass-by-reference and pass-by-value to variadic template function valid?

故事扮演 提交于 2021-02-08 14:05:46
问题 I have a method which allocates memory for a object and then calls its constructor - a memory allocator. template <class T, typename... Arguments> inline T* AllocateObject(Arguments... args) { return new (InternalAllocate(sizeof(T))) T(args...); } Is it valid using this function to mix pass-by-value and pass-by-reference? For example allocating a class with a constructor with some by-value and some by-reference. It compiles, but I'm not sure if it has any nasty side-effects or not. 回答1: What

How does boost::serialization allocate memory when deserializing through a pointer?

假如想象 提交于 2021-01-28 11:51:08
问题 In short, I'd like to know how boost::serialization allocates memory for an object when deserializing through a pointer. Below, you'll find an example of my question, clearly illustrated alongside companion code. This code should be fully functional and compile fine, there are no errors, per se, just a question on how the code actually works. #include <cstddef> // NULL #include <iomanip> #include <iostream> #include <fstream> #include <string> #include <boost/archive/text_iarchive.hpp>

Placement new base subobject of derived in C++

断了今生、忘了曾经 提交于 2020-07-09 08:26:04
问题 Is it defined behavior to placement-new a trivially destructible base object of a derived? struct base { int& ref; }; struct derived : public base { complicated_object complicated; derived(int& r, complicated_arg arg) : base {r}, complicated(arg) {} }; unique_ptr<derived> rebind_ref(unique_ptr<derived>&& ptr, int& ref) { // Change where the `ref` in the `base` subobject of // derived refers. return unique_ptr<derived>(static_cast<derived*>( ::new (static_cast<base*>(ptr.release()) base{ref}))

Placement new base subobject of derived in C++

橙三吉。 提交于 2020-07-09 08:23:05
问题 Is it defined behavior to placement-new a trivially destructible base object of a derived? struct base { int& ref; }; struct derived : public base { complicated_object complicated; derived(int& r, complicated_arg arg) : base {r}, complicated(arg) {} }; unique_ptr<derived> rebind_ref(unique_ptr<derived>&& ptr, int& ref) { // Change where the `ref` in the `base` subobject of // derived refers. return unique_ptr<derived>(static_cast<derived*>( ::new (static_cast<base*>(ptr.release()) base{ref}))

How to delete object constructed via placement new operator?

▼魔方 西西 提交于 2020-01-27 15:56:02
问题 char * buf = new char[sizeof(T)]; new (buf) T; T * t = (T *)buf; //code... //here I should destruct *t but as it is argument of template and can be //instantiated via basic types as well (say int) so such code /*t->~T();*/ //is incorrect (maybe correct? Strange, but it works on VS 2005 for basic types.) //and this code /*delete t;*/ //crashes the program. delete [] buf; So what is correct way to destruct t ? P.S. The code above is only for describing my problem, and have not real relationship

How to delete object constructed via placement new operator?

陌路散爱 提交于 2020-01-27 15:54:29
问题 char * buf = new char[sizeof(T)]; new (buf) T; T * t = (T *)buf; //code... //here I should destruct *t but as it is argument of template and can be //instantiated via basic types as well (say int) so such code /*t->~T();*/ //is incorrect (maybe correct? Strange, but it works on VS 2005 for basic types.) //and this code /*delete t;*/ //crashes the program. delete [] buf; So what is correct way to destruct t ? P.S. The code above is only for describing my problem, and have not real relationship