Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using placement new

Reply
Thread Tools

using placement new

 
 
REH
Guest
Posts: n/a
 
      03-27-2005
Hi. I want to have a union that contained various types, some of which are
classes with constructors. I'm attempting to acheive this with placement
new. Please tell me if the following code snippet meets the current
standard. Should I cast char* to void* before casting to std::string*?

Regards,

REH


class entry {
public:
enum entry_type {
none, str
};

entry() : m_type(none) {}

entry(const std::string& s) : m_type(str) {new(m_data.str)
std::string(s);}

// note: my compiler didn't like the use of std::string below.
// it would only accept using a typedef of std::string or a using
clause.
// specifically, it didn't like the ~string(). why?
~entry() {using std::string; if (m_type == str)
reinterpret_cast<string*>(m_data.str)->~string();}

entry_type get_type() const {return m_type;}

std::string get_str() const
{
if (m_type == str)
*reinterpret_cast<string*>(m_data.str);
else
throw format_error(); // defined elsewhere
}

private:
entry_type m_type;

union {
double dbl; // ensure any necessary alignment of types
char str[sizeof(std::string)];
} m_data;
};


 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      03-27-2005
REH wrote:

> Hi. I want to have a union that contained various types, some of which

are
> classes with constructors.


What's wrong with a union of primitives and of pointers to types with
constructors?

Look up a "variant" class.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


 
Reply With Quote
 
 
 
 
REH
Guest
Posts: n/a
 
      03-28-2005

"REH" <(E-Mail Removed)> wrote in message
news:fHB1e.105690$(E-Mail Removed)...
> Hi. I want to have a union that contained various types, some of which

are
> classes with constructors. I'm attempting to acheive this with placement
> new. Please tell me if the following code snippet meets the current
> standard. Should I cast char* to void* before casting to std::string*?
>
> Regards,
>
> REH
>
>
> class entry {
> public:
> enum entry_type {
> none, str
> };
>
> entry() : m_type(none) {}
>
> entry(const std::string& s) : m_type(str) {new(m_data.str)
> std::string(s);}
>
> // note: my compiler didn't like the use of std::string below.
> // it would only accept using a typedef of std::string or a using
> clause.
> // specifically, it didn't like the ~string(). why?
> ~entry() {using std::string; if (m_type == str)
> reinterpret_cast<string*>(m_data.str)->~string();}
>
> entry_type get_type() const {return m_type;}
>
> std::string get_str() const
> {
> if (m_type == str)
> *reinterpret_cast<string*>(m_data.str);
> else
> throw format_error(); // defined elsewhere
> }
>
> private:
> entry_type m_type;
>
> union {
> double dbl; // ensure any necessary alignment of types
> char str[sizeof(std::string)];
> } m_data;
> };
>
>


Anyone see any portability and/or standard issues with the above?

Thanks a lot.


 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-28-2005
REH wrote:

> Hi. I want to have a union that contained various types, some of which are
> classes with constructors.



As TC++PL is mentioning:


"10.4.12 Unions

A named union is defined as a struct, where every member has the same address (see C.8.2).
A union can have member functions but not static members.

In general, a compiler cannot know what member of a union is used; that is, the type of
the object stored in a union is unknown. Consequently, a union may not have members with
constructors or destructors. It wouldn’t be possible to protect that object against
corruption or to guarantee that the right destructor is called when the union goes out of
scope.

Unions are best used in low-level code, or as part of the implementation of classes that
keep track of what is stored in the union (see 10.6[20]).




--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      03-28-2005

"Ioannis Vranos" <(E-Mail Removed)> wrote in message
news:1112043107.612258@athnrd02...
> REH wrote:
>
> > Hi. I want to have a union that contained various types, some of which

are
> > classes with constructors.

>
>
> As TC++PL is mentioning:
>
>
> "10.4.12 Unions
>
> A named union is defined as a struct, where every member has the same

address (see C.8.2).
> A union can have member functions but not static members.
>
> In general, a compiler cannot know what member of a union is used; that

is, the type of
> the object stored in a union is unknown. Consequently, a union may not

have members with
> constructors or destructors. It wouldn’t be possible to protect that

object against
> corruption or to guarantee that the right destructor is called when the

union goes out of
> scope.
>
> Unions are best used in low-level code, or as part of the implementation

of classes that
> keep track of what is stored in the union (see 10.6[20]).
>
>

Yes, I know that. The statement you commented on was just a preface to
explaining what I was trying to acheive.


 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-28-2005
REH wrote:

> Anyone see any portability and/or standard issues with the above?



Your code made to compile. You may check the comments:


#include <string>


class format_error {};

class entry {
public:
enum entry_type {
none, str
};

entry() : m_type(none) {}

//==> Non-portable operation. See below.
entry(const std::string &s) : m_type(str) {new(m_data.str)
std::string(s);}

// note: my compiler didn't like the use of std::string below.
// it would only accept using a typedef of std::string or a using clause.
// specifically, it didn't like the ~string(). why?
~entry() {using std::string; if (m_type == str)
(reinterpret_cast<string *>(m_data.str))->~string();}

entry_type get_type() const {return m_type;}

std::string get_str()
{
if (m_type == str)
*reinterpret_cast<std::string *>(m_data.str);
else
throw format_error(); // defined elsewhere
}

private:
entry_type m_type;

union {
double dbl; // ensure any necessary alignment of types

// ==> Made unsigned char since std::string is a non-POD type
// ==> just to give it some more chance.
// ==> Since std::string is a non-POD type though, this code is not
// ==> portable.
unsigned char str[sizeof(std::string)];
} m_data;
};


int main()
{
}


--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using placement new to re-initialize part of an object removeps-generic@yahoo.com C++ 5 04-05-2006 08:12 PM
using placement new to re-initialize part of an object removeps-generic@yahoo.com C++ 0 04-04-2006 09:24 PM
using placement new to re-initialize part of an object removeps-generic@yahoo.com C++ 0 04-04-2006 09:24 PM
using placement new to forward ctor calls Marc Mutz C++ 6 07-19-2005 08:42 PM
overriding operator new and accessing placement new Mark P C++ 6 04-27-2005 04:17 AM



Advertisments