specialize a template for bitset<N>

S

shaun

I have a function for returning the value of a bit field in a number:

template<typename T>
T bitfield(const T num, const unsigned int bitStart, const unsigned int
bitEnd){
T mask, shiftedNumber;
unsigned int maskLength = bitEnd-bitStart+1;
shiftedNumber = num>>bitStart;
mask = (1<<maskLength)-1;
return (shiftedNumber & mask);
};


I should like to specialize this for a bitset such that it always
returns an unsigned int, but a bitset<32> is not the same as a
bitset<16>... groping around a bit, I am trying:

template <int N>
unsigned int bitfield(const bitset<N> num, const unsigned int bitStart,
const unsigned int bitEnd){
unsigned int mask, shiftedNumber;
unsigned int maskLength = bitEnd-bitStart+1;
shiftedNumber = num>>bitStart;
mask = (1<<maskLength)-1;
return (shiftedNumber & mask);
};

but all I get is:

In file included from /Volumes/Shauns stuff/Developer/learning
C++/bitfield/main.cpp:2:
/Volumes/Shauns stuff/Developer/learning C++/bitfield/bitfield.hpp:21:
error: parse error before `,' token


where line 21 is the first line of my function specialized for bitset.

Is there a way to do this?

cheers

shaun
 
V

Victor Bazarov

shaun said:
I have a function for returning the value of a bit field in a number:

template<typename T>
T bitfield(const T num, const unsigned int bitStart, const unsigned int
bitEnd){
T mask, shiftedNumber;
unsigned int maskLength = bitEnd-bitStart+1;
shiftedNumber = num>>bitStart;
mask = (1<<maskLength)-1;
return (shiftedNumber & mask);
};


I should like to specialize this for a bitset such that it always
returns an unsigned int, but a bitset<32> is not the same as a
bitset<16>... groping around a bit, I am trying:

template <int N>
unsigned int bitfield(const bitset<N> num, const unsigned int bitStart,
const unsigned int bitEnd){
unsigned int mask, shiftedNumber;
unsigned int maskLength = bitEnd-bitStart+1;
shiftedNumber = num>>bitStart;
mask = (1<<maskLength)-1;
return (shiftedNumber & mask);
};

but all I get is:

In file included from /Volumes/Shauns stuff/Developer/learning
C++/bitfield/main.cpp:2:
/Volumes/Shauns stuff/Developer/learning C++/bitfield/bitfield.hpp:21:
error: parse error before `,' token


where line 21 is the first line of my function specialized for bitset.

Is there a way to do this?

'bitset' is inside the 'std' namespace. Next time post the entire code
because without it we cannot know for sure whether you've included the
right header and helped your compiler to find the 'bitset' template.

V
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top