bitsets in C++ using VS2010

2020-04-10 02:55发布

问题:

I can't figure out if I'm doing anything wrong here, hopefully someone here can enlighten me.

I have a class Flags, this is an extremely simplified version but I declare a bitset

class Flags
{
private:
    //List of 8 bits
    std::bitset<8> _P;
public:
    Flags();
}

On my constructor I initialise it as

Flags::Flags()
    : _P(32ul)
{}

But it won't compile and gives me the error

error C2668: 'std::bitset<_Bits>::bitset' : ambiguous call to overloaded function

This is compiled in VS2010 SP1 64 bit but as a 32bit program

EDIT

Accepted answer is for the above but as a side note could anyone explain why when using the default constructor (which should initialise them all to zero's) they aren't all set to zero's?

_p.to_ulong()

returns 1390560944 and _p looks like

[8](0,0,0,0,1,1,0,1)

回答1:

It's a bug in VC according to http://connect.microsoft.com/VisualStudio/feedback/details/532897/problems-constructing-a-bitset-from-an-unsigned-long-in-the-vc-rc

Also note that identifiers starting with _ and a capital letter are reserved for the implementation and illegal to use in your program.

EDIT: According to the workaround page (if I read it right), the workaround is to cast your value to unsigned long long instead of unsigned long.