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

std::bitset in union

I would wery much like this to work:

@code start
#include <iostream>
#include <bitset>
const unsigned short size = 2; // 2^<unsigned int> //
union V {
char c[(size*size*size)/8];
std::bitset<size*size*size> b;
} v;
@code end

The compiler wont accept it and i cant accept that ;-)
A far as im concersed a bitset is just a number of bits and it shouldnt
be a problem, however it is and im in great need of a solution.

compiler error: "member std::bitset<8u> V::b' with constuctor not
allowed in union"

Feb 15 '06 #1
7 8157
felixniel...@hotmail.com wrote:
I would wery much like this to work:

@code start
#include <iostream>
#include <bitset>
const unsigned short size = 2; // 2^<unsigned int> //
union V {
char c[(size*size*size)/8];
std::bitset<size*size*size> b;
} v;
@code end

The compiler wont accept it and i cant accept that ;-)
A far as im concersed a bitset is just a number of bits and it shouldnt
be a problem, however it is and im in great need of a solution.
compiler error: "member std::bitset<8u> V::b' with constuctor not
allowed in union"


Why don't you explain why you need a union. Unions are rarely needed
in C++, in that generally you can substitute some C++ idiom instead.
So, let us know, and I bet we can find you a solution.

Best regards,

Tom

Feb 15 '06 #2

<fe**********@hotmail.com> skrev i meddelandet
news:11*********************@g14g2000cwa.googlegro ups.com...
I would wery much like this to work:

@code start
#include <iostream>
#include <bitset>
const unsigned short size = 2; // 2^<unsigned int> //
union V {
char c[(size*size*size)/8];
std::bitset<size*size*size> b;
} v;
@code end

The compiler wont accept it and i cant accept that ;-)
Too bad. :-)
A far as im concersed a bitset is just a number of bits and it
shouldnt
be a problem, however it is and im in great need of a solution.
It's an object. The internal implementation is not specified.

compiler error: "member std::bitset<8u> V::b' with constuctor not
allowed in union"


Exactly.

However,
if you keep the size within moderate ranges, there is a constructor
bitset(unsigned long) that sets the bits from an unsigned long. There
is also the opposite conversion bitset::to_ulong(), that gets you as
many bits as will fit.

As the size of an unsigned long is not fixed (but at least 32 bits) it
might solve just some of your problems.
Bo Persson
Feb 15 '06 #3
fe**********@hotmail.com wrote:
I would wery much like this to work:

@code start
#include <iostream>
#include <bitset>
const unsigned short size = 2; // 2^<unsigned int> //
union V {
char c[(size*size*size)/8];
std::bitset<size*size*size> b;
} v;
@code end

The compiler wont accept it and i cant accept that ;-)
A far as im concersed a bitset is just a number of bits and it shouldnt
be a problem, however it is and im in great need of a solution.

compiler error: "member std::bitset<8u> V::b' with constuctor not
allowed in union"


I found this thread:
http://groups.google.com/group/comp....2a43450544df81

In Ron Ruble's post, he quotes the standard (section 9.5):

"An object of a class with a non-trivial constructor (12.1), a
non-trivial destructor (12.4), or a non-trivial copy constructor
(13.5.3, 12.8) cannot be a member of a union"

--
Marcus Kwok
Feb 15 '06 #4
> fe**********@hotmail.comwrote:
I would wery much like this to work:

@code start
#include <iostream>
#include <bitset>
const unsigned short size = 2; // 2^<unsigned int> //
union V {
char c[(size*size*size)/8];
std::bitset<size*size*size> b;
} v;
@code end

The compiler wont accept it and i cant accept that ;-)
A far as im concersed a bitset is just a number of bits and it shouldnt be a problem, however it is and im in great need of a solution.

compiler error: "member std::bitset<8u> V::b' with constuctor not allowed in union"


Dear Sir, C++ union is a little bit too useless because it supports
only POD types. In your case you have std::bitset which is not a POD
type. There are many solutions, but the best I can recommend is to
use "boost::variant". Variant template in boost is a safe, generic,
stack-based discriminated union container, offering a simple solution
for manipulating an object from a heterogeneous set of types in a
uniform manner. That's what you need. Read more at
http://www.boost.org/doc/html/variant.html

Wish best regards,
Vladislav Lazarenko.

Feb 15 '06 #5
> fe**********@hotmail.comwrote:
I would wery much like this to work:

@code start
#include <iostream>
#include <bitset>
const unsigned short size = 2; // 2^<unsigned int> //
union V {
char c[(size*size*size)/8];
std::bitset<size*size*size> b;
} v;
@code end

The compiler wont accept it and i cant accept that ;-)
A far as im concersed a bitset is just a number of bits and it shouldnt be a problem, however it is and im in great need of a solution.

compiler error: "member std::bitset<8u> V::b' with constuctor not allowed in union"


Dear Sir, C++ union is a little bit too useless because it supports
only POD types. In your case you have std::bitset which is not a POD
type. There are many solutions, but the best I can recommend is to
use "boost::variant". Variant template in boost is a safe, generic,
stack-based discriminated union container, offering a simple solution
for manipulating an object from a heterogeneous set of types in a
uniform manner. That's what you need. Read more at
http://www.boost.org/doc/html/variant.html

Wish best regards,
Vladislav Lazarenko.

Feb 15 '06 #6
I have done some thingking and i now realize that i dont actually need
this, however i still think it could be usefull when manipulating bit
in wery large numbers.

Otherwise im thankfull for your help.

Regards
Zacariaz

Feb 15 '06 #7
fe**********@hotmail.com wrote:
I have done some thingking and i now realize that i dont actually need
this, however i still think it could be usefull when manipulating bit
in wery large numbers.

Otherwise im thankfull for your help.


As a side note -- if you had planned to use this union
to 'convert' the bitset to char[] or vice versa, you'd
better be warned that it's illegal to do that using a
union.

HTH,
- J.
Feb 16 '06 #8

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

Similar topics

2
by: Dill Hole | last post by:
Can anyone tell me why std::bitset<2> foo(std::string("01")) initializes the bitset in reverse order, i.e. foo=true foo=false I would expect the bitset to be initialized in string text...
5
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): ...
3
by: Gaijinco | last post by:
In C++ this code: for(int i=0; i<16; ++i){ std::cout << std::bitset<4>(i) << std::endl; Will print numbers 1-15 as binaries with 4 bits. Is there any equivalent of bitset in C?
14
by: Haro Panosyan | last post by:
How to construct a bitset from string? For example: std::bitset<16> b16("1011011110001011"); Is this possible? Thanks in advance. -haro
5
by: Sean Farrow | last post by:
Hi: I have an iterator defined as follows: std::map<std::bitset<6>, int>::iterator DotsIterator, SignsIterator; I get errors that std::bitset does not declare the < operator. Does this mean I...
3
by: Guy.Tristram | last post by:
Is there any good reason operator< is not defined for std::bitset? It seems to me that: 1 - it would be useful. 2 - it is easy to implement inside the class template. 3 - it is impossible to...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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,...

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.