473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1694
* 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::MyClass(...)
{
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
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...
20
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
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
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...
5
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...
13
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...
6
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 *...
3
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...);...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.