By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,795 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,795 IT Pros & Developers. It's quick & easy.

specialize a template for bitset<N>

P: n/a
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
Mar 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
where line 21 is the first line of my function specialized for
bitset.


post this line. It's got a bug/typo.
Mar 16 '06 #2

P: n/a
shaun wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 16 '06 #3

P: n/a
forward this post to comp.lang.c++.farseers ;-)

Mar 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.