473,662 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using placement new to forward ctor calls

Hi,

I'm in discussion with a lib vendor who uses placement new
to forward construction to another ctor, like this:
MyClass( ... ) {
new (this) MyClass( ... );
}
I asked them to use a private init() function instead, but
they claim:
you might be correct that using placement new to forward
construction is not well defined in the general case. In
our case however it is, as the class only contains one
pimpl-pointer (no members that have a constructor
and no vtable). In this case it's perfectly valid to use
the construct we're using.


Please supply me with ammonition to make them reconsider
(or tell me they're right :/).

Thanks,
Marc

Jul 23 '05 #1
6 1709
* Marc Mutz:

I'm in discussion with a lib vendor who uses placement new
to forward construction to another ctor, like this:
MyClass( ... ) {
new (this) MyClass( ... );
}
I asked them to use a private init() function instead, but
they claim:
you might be correct that using placement new to forward
construction is not well defined in the general case. In
our case however it is, as the class only contains one
pimpl-pointer (no members that have a constructor
and no vtable). In this case it's perfectly valid to use
the construct we're using.


Please supply me with ammonition to make them reconsider
(or tell me they're right :/).


"Right" or "wrong" is not really relevant wrt. a platform-specific library.
Either it works, or not. The technical problem is whether it works with all
relevant compilers (it may, or it may not), and with future versions of
those compilers (it may not). The problem for a client is that the
construct above is _unnecessary_ UB, and bodes not well for the quality of
the rest of the library implementation. It indicates that they're "clever"
technicians, not engineers; an engineer wouldn't do a thing like that.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Marc Mutz wrote:
Hi,

I'm in discussion with a lib vendor who uses placement new
to forward construction to another ctor, like this:
MyClass( ... ) {
new (this) MyClass( ... );
}
I asked them to use a private init() function instead, but
they claim:
you might be correct that using placement new to forward
construction is not well defined in the general case. In
our case however it is, as the class only contains one
pimpl-pointer (no members that have a constructor
and no vtable). In this case it's perfectly valid to use
the construct we're using.


Please supply me with ammonition to make them reconsider
(or tell me they're right :/).


1) It's not easy to understand, it's not clear what this code is trying
to achieve
2) It's not common practice
3) It's brittle, it may break on another system/compiler/version.
4) It may be wrong (is this really a MyClass?)
5) It may be less "efficient" than calling an init() function
6) It is illegal
7) The fact that they admit "using placement new to forward
construction is not well defined in the general case" is quite scary.

And 8) change the library.
Jonathan

Jul 23 '05 #3
The only thing I have to add to the superior answers you have received
so far is that I am not accustomed to being born twice in spite of
dying once. And it's UB,thus you can expect demons, aliens, formatted
drives and absense of errors.

Jul 23 '05 #4
leonardo77 wrote:
And it's UB,thus you can expect demons, aliens, formatted
drives and absense of errors.


Where in the standard is that requirement? <g> In fact, I expect that it
does exactly what it was described as doing. With suitable knowledge of
the target platform and of the compiler and with appropriate testing
there's no need to fear code whose behavior isn't defined by the
language definition.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #5
Marc Mutz wrote:
I'm in discussion with a lib vendor who uses placement new
to forward construction to another ctor, like this:
MyClass( ... ) {
new (this) MyClass( ... );
}
Please supply me with ammonition to make them reconsider
(or tell me they're right :/).


It's uncool. It looks as if they dont know what they are doing.

------
"The fact that the program works has no relevance."
Bartosz Milewski

Jul 23 '05 #6
Me
Marc Mutz wrote:
I'm in discussion with a lib vendor who uses placement new
to forward construction to another ctor, like this:
MyClass( ... ) {
new (this) MyClass( ... );
}
I asked them to use a private init() function instead, but
they claim:
you might be correct that using placement new to forward
construction is not well defined in the general case. In
our case however it is, as the class only contains one
pimpl-pointer (no members that have a constructor
and no vtable). In this case it's perfectly valid to use
the construct we're using.


Please supply me with ammonition to make them reconsider
(or tell me they're right :/).


This is already in shaky grounds because the lifetime of a non-POD
before construction completes and before destruction begins is really
hard to reason about and there are lots of restrictions on what is well
defined and what isn't (see 3.8 and the sections it references for more
details). But if we ignore that and pick a simpler reason why this code
is illegal:

3.8/1 "The lifetime of an object of type T begins when ... if T is a
class type and the constructor invoked to create the object is
non-trivial (12.1), the constructor call has completed."

MyClass::MyClas s(...)
{
new ((void*)this) MyClass(...); //1
} //2

So what happens here is that they are using placement new to create a
new object at the location pointed to by this //1. This creates a new
MyClass object, that's fine (totally ignoring the issues I talked about
above). The problem occurs when the MyClass ctor returns //2, the
constructor call has completed, which means *two* MyClass objects are
created at the same memory location, which is illegal since MyClass is
not a POD struct. Since there is no way to forward ctor calls in the
current standard, the only legal thing to do is call an init()
function.

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2049
by: Martin Eisenberg | last post by:
Hi! I have a header with two classes, WidthLogger and Hyst. WidthLogger is declared first; its ctor takes a Hyst reference. Hyst declares WidthLogger a friend. Hyst constructs a WidthLogger member, giving itself as parameter, and calls its log() method in various places. WidthLogger::log() then uses the reference to note down the values of some Hyst members. Except that I don't get that far because VC6 is bitchin', I hope
20
4139
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
10
1354
by: richardclay09 | last post by:
The output of this: #include <iostream> #include <vector> using namespace std; struct X { int i; X(const X& x) : i(x.i) { cout << "ctor copy: " << i << endl;
3
1885
by: ma740988 | last post by:
If I understand placement new. Object destruction requires an explicit call to the destructor. struct my_struct { unsigned int val1 : 5; unsigned int val2 : 4; unsigned int reserved : 23; }; Now given an address - say 0x40000000. For the case where I have bit
5
4569
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public constructors (because we do not allow developers to instantiate them directly). Because our business objects are instantiated very frequently, the idea of using reflection sounds like a performance killer (I haven't done any tests on this, but the...
13
9706
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not initialized properly as constructor same is not get called ( as per the behavior). How to call a constructor explicitly if we want to allocate memory using malloc ?
6
5705
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
3
1763
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...); 3: delete p; line 2 I know it should be fine because global operator new should return
11
2278
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.