By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

using placement new

P: n/a
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;
};
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
REH

"REH" <me@you.com> wrote in message
news:fH*******************@twister.nyroc.rr.com...
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.
Jul 23 '05 #3

P: n/a
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
Jul 23 '05 #4

P: n/a
REH

"Ioannis Vranos" <iv*@remove.this.grad.com> 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.
Jul 23 '05 #5

P: n/a
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
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.