473,395 Members | 1,656 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.

Placement new and templates

Hello.

The following code compiles fine with VC7. I seemingly call int's
destructor. Why does it compile ? What does the standard say about this
?

#include <new>

using namespace std;

template <typename T>
void destruct(T* that)
{
that->~T();
}

int main()
{
char memory[100];
int* a = 0;

// Can do this...
a = new(memory) int;
destruct(a);

// But can't do this
a = new(memory) int;
//a->~int();

return 0;
}

Jul 23 '05 #1
4 1794
Kalle Rutanen wrote:
The following code compiles fine with VC7. I seemingly call int's
destructor. Why does it compile ? What does the standard say about
this ?

It's explicitly allowed. It's called "pseudo-destructor call". The
Standard defines that in 5.2.4. If your compiler does not allow you
do

int *pa;
...
pa->~int();

then your compiler is broken. For built-in types a pseudo-destructor
call has no effect except to evaluate the expression before the . or
the ->.

V
Jul 23 '05 #2

Victor Bazarov wrote:
If your compiler does not allow you
do

int *pa;
...
pa->~int();

then your compiler is broken. For built-in types a pseudo-destructor
call has no effect except to evaluate the expression before the . or
the ->.


So a pedantic compiler may reject this as UB, considering
that you're dereferencing an uinitialized pointer ;)

Of course, normal compilers will just skip this instruction.

Regards,
Michiel Salters

Jul 23 '05 #3
msalters wrote:
Victor Bazarov wrote:
If your compiler does not allow you
do

int *pa;
...
pa->~int();

then your compiler is broken. For built-in types a pseudo-destructor
call has no effect except to evaluate the expression before the . or
the ->.

So a pedantic compiler may reject this as UB, considering
that you're dereferencing an uinitialized pointer ;)


The '...' contains all the necessary code to prevent the UB.

V
Jul 23 '05 #4
> It's explicitly allowed. It's called "pseudo-destructor call". The
Standard defines that in 5.2.4. If your compiler does not allow you
do

int *pa;
...
pa->~int();

then your compiler is broken. For built-in types a pseudo-destructor
call has no effect except to evaluate the expression before the . or
the ->.


Thank you for a clearing answer.
Jul 23 '05 #5

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

Similar topics

23
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
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>
8
by: elviin | last post by:
Hello. I tried a sample programm using placement new. I am not sure if i understand this technique correctly. I do not mean usage but a real world application. Could anyone to describe some...
5
by: ma740988 | last post by:
I've got a struct which is comprised of POD types. I know in advance where in memory the struct resides. To make a long story short, I'm doing transfers of 16 bit ADC sampled data between...
15
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
1
by: SarahT | last post by:
Hi folks, I am doing something Very Bad and Wrong (which I'll spare you the details of) that requires overloading new for some specific classes. So, for example: class MyWeirdThingy {...
5
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
19
by: Juha Nieminen | last post by:
Assume I have a class Base, and then this: struct Derived: public Base { AnotherClass member; }; Also assume that I'm allocating an object of type 'Derived' with a custom allocator and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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.