using placement new

R

REH

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;
};
 
P

Phlip

REH said:
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.
 
R

REH

REH said:
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.
 
I

Ioannis Vranos

REH said:
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]).
 
R

REH

Ioannis Vranos said:
REH said:
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.
 
I

Ioannis Vranos

REH said:
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()
{
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top