std::bitset in union

F

felixnielsen

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"
 
T

Thomas Tutone

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
 
B

Bo Persson

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
 
M

Marcus Kwok

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...1?q=union+constructor&rnum=2#2f2a43450544df81

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"
 
J

JetSnaiL

(e-mail address removed)
I would wery much like this to work
@code star
#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 en

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

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

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

Wish best regards
Vladislav Lazarenko
 
J

JetSnaiL

(e-mail address removed)
I would wery much like this to work
@code star
#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 en

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

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

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

Wish best regards
Vladislav Lazarenko
 
F

felixnielsen

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
 
J

Jacek Dziedzic

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.
 

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,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top