473,554 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making a std::string a member of a union ???

Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};
Jan 9 '07 #1
84 15804
"Peter Olcott" <No****@SeeScre en.comwrote in news:ZtPoh.8718 $rv1.8337
@newsfe21.lga:
Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};

You can't do this even with your own string:

Section 12.1.11: "A union member shall not be of a class type (or array
thereof) that has a non-trivial constructor.".
Jan 9 '07 #2
Peter Olcott schrieb:
Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};
You cannot. But depending on what you want to do, you might find
boost::variant or boost::any quite useful:

http://www.boost.org/doc/html/variant.html
http://www.boost.org/doc/html/any.html

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jan 9 '07 #3

"Andre Kostur" <nn******@kostu r.netwrote in message
news:Xn******** *************** ********@209.13 5.99.21...
"Peter Olcott" <No****@SeeScre en.comwrote in news:ZtPoh.8718 $rv1.8337
@newsfe21.lga:
>Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};


You can't do this even with your own string:

Section 12.1.11: "A union member shall not be of a class type (or array
thereof) that has a non-trivial constructor.".
If I create my own StringType class that has every other feature of std::string,
except nontrivial constructors, then it should work?

For example if every instance of StringType is always empty unless data is
explicitly added using operator=() or other means, then there would seem to be
no need for constructors.
Jan 9 '07 #4
"Peter Olcott" <No****@SeeScre en.comwrote in
news:1q******** ***********@new sfe24.lga:
>
"Andre Kostur" <nn******@kostu r.netwrote in message
news:Xn******** *************** ********@209.13 5.99.21...
>"Peter Olcott" <No****@SeeScre en.comwrote in
news:ZtPoh.871 8$rv1.8337 @newsfe21.lga:
>>Is there anyway of doing this besides making my own string from
scratch?

union AnyType {
std::string String;
double Number;
};


You can't do this even with your own string:

Section 12.1.11: "A union member shall not be of a class type (or
array thereof) that has a non-trivial constructor.".

If I create my own StringType class that has every other feature of
std::string, except nontrivial constructors, then it should work?

For example if every instance of StringType is always empty unless
data is explicitly added using operator=() or other means, then there
would seem to be no need for constructors.
Watch out if the members of your class have non-trivial constructors. What
I don't know offhand (hopefully someone else can elaborate), are simply
initializations enough to call it a non-trivial constructor? Ex:

class Simple
{
public:
Simple() : data(0), size(0) {};

private:
char * data;
size_t size;
};
Is that a non-trivial constructor?
Jan 9 '07 #5

"Andre Kostur" <nn******@kostu r.netwrote in message
news:Xn******** *************** ********@209.13 5.99.21...
"Peter Olcott" <No****@SeeScre en.comwrote in
news:1q******** ***********@new sfe24.lga:
>>
"Andre Kostur" <nn******@kostu r.netwrote in message
news:Xn******* *************** *********@209.1 35.99.21...
>>"Peter Olcott" <No****@SeeScre en.comwrote in
news:ZtPoh.87 18$rv1.8337 @newsfe21.lga:

Is there anyway of doing this besides making my own string from
scratch?

union AnyType {
std::string String;
double Number;
};
You can't do this even with your own string:

Section 12.1.11: "A union member shall not be of a class type (or
array thereof) that has a non-trivial constructor.".

If I create my own StringType class that has every other feature of
std::string, except nontrivial constructors, then it should work?

For example if every instance of StringType is always empty unless
data is explicitly added using operator=() or other means, then there
would seem to be no need for constructors.

Watch out if the members of your class have non-trivial constructors. What
I don't know offhand (hopefully someone else can elaborate), are simply
initializations enough to call it a non-trivial constructor? Ex:

class Simple
{
public:
Simple() : data(0), size(0) {};

private:
char * data;
size_t size;
};
Is that a non-trivial constructor?
I think that anything besides Simple(){}; is a non trivial constructor. This is
as trivial as trivial gets, syntax that is empty of semantics.
Jan 9 '07 #6
Peter Olcott wrote:
>
"Andre Kostur" <nn******@kostu r.netwrote in message
news:Xn******** *************** ********@209.13 5.99.21...
>"Peter Olcott" <No****@SeeScre en.comwrote in news:ZtPoh.8718 $rv1.8337
@newsfe21.lg a:
>>Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};


You can't do this even with your own string:

Section 12.1.11: "A union member shall not be of a class type (or array
thereof) that has a non-trivial constructor.".

If I create my own StringType class that has every other feature of
std::string, except nontrivial constructors, then it should work?

For example if every instance of StringType is always empty unless data is
explicitly added using operator=() or other means, then there would seem
to be no need for constructors.
Sorry, but no. 9.5 says: "An object of a class with a non-trivial
constructor, a non-trivial copy constructor, a non-trivial destructor, or a
non-trivial copy assignment operator cannot be a member of a union, nor can
an array of such objects."

Jan 9 '07 #7
Peter Olcott wrote:
\
>
I think that anything besides Simple(){}; is a non trivial constructor. This is
as trivial as trivial gets, syntax that is empty of semantics.

Nope, even that is a non-trivial constructor.

A trivial constructor means that there is NONE of the
following:
1. No implicitly-declared default constructors
2. No virtual functions
3. No virtual base classes
4. All direct base classes have trivial constructors
5. All non-static members have trivial constructors
Jan 9 '07 #8

"Rolf Magnus" <ra******@t-online.dewrote in message
news:eo******** *****@news.t-online.com...
Peter Olcott wrote:
>>
"Andre Kostur" <nn******@kostu r.netwrote in message
news:Xn******* *************** *********@209.1 35.99.21...
>>"Peter Olcott" <No****@SeeScre en.comwrote in news:ZtPoh.8718 $rv1.8337
@newsfe21.lga :

Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};
You can't do this even with your own string:

Section 12.1.11: "A union member shall not be of a class type (or array
thereof) that has a non-trivial constructor.".

If I create my own StringType class that has every other feature of
std::string, except nontrivial constructors, then it should work?

For example if every instance of StringType is always empty unless data is
explicitly added using operator=() or other means, then there would seem
to be no need for constructors.

Sorry, but no. 9.5 says: "An object of a class with a non-trivial
constructor, a non-trivial copy constructor, a non-trivial destructor, or a
non-trivial copy assignment operator cannot be a member of a union, nor can
an array of such objects."

Well then how can I make a union of AnyType that includes something like a
std::string as one of its members?
Jan 9 '07 #9

"Ron Natalie" <ro*@spamcop.ne twrote in message
news:45******** *************** @news.newshosti ng.com...
Peter Olcott wrote:
\
>>
I think that anything besides Simple(){}; is a non trivial constructor. This
is as trivial as trivial gets, syntax that is empty of semantics.

Nope, even that is a non-trivial constructor.
I think that you must be wrong on this issue, you can't possibly get more
trivial than syntax that is completely empty of corresponding semantics.
A trivial constructor means that there is NONE of the
following:
1. No implicitly-declared default constructors
2. No virtual functions
3. No virtual base classes
4. All direct base classes have trivial constructors
5. All non-static members have trivial constructors

Jan 9 '07 #10

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

Similar topics

6
10856
by: prettysmurfed | last post by:
Hi all I have a bit of a problem, the subject of this post is almost selfexplaing. But here goes: Heres an example of the code I want to implement, its all nice and simple, but the flaw is I can't seem to get the adress of the member function stated properly in the DialogBox function. I thought it was enough to write it as Dialog::DlgProc,...
5
9377
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. what does a private static member function mean? Thaanx in advance for any advice Rgds,
7
2482
by: Steven T. Hatton | last post by:
I am trying to convert some basic OpenGL code to an OO form. This is the C version of the program: http://www.opengl.org/resources/code/basics/redbook/double.c You can see what my current effort at converting that code to an OO form looks like in the code listed below. The problem I'm running into is that the OpenGL functions that take...
18
2368
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and compiler), and it is, what I conclude from the below code union data_type {
9
1380
by: Richard Lewis Haggard | last post by:
What is the logic for dropping the C++ prepending of 'm_' from member variables? Has there been a consensus developed on how to discriminate between class members and automatic temp variables? -- Richard Lewis Haggard
11
4392
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI components. The application is built around a "World" object that contains a "GUI" object that is a C++ wrapper around GLUT. What I'd like to do is pass a...
5
1575
by: mailforpr | last post by:
Hello. Given is the following class: class Test { public: int i; int& get() { return i; } };
2
1235
by: Zytan | last post by:
I want to extend an existing class (a standard control), by making more member functions, to improve the ease of its use. *I don't want to make my own control, or add new functionality.* I just want to improve the *interface* for existing functionality. So, instead of doing this: MyUnit.MyFunction(Control, ...) I want:...
6
2574
by: steve.kim | last post by:
Hello, I'm trying to make a class like below... class myClass { public: // ctor / dtor .... // methods
7
2177
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
0
7535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7812
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7574
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7894
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5162
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3579
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3561
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2026
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.