473,396 Members | 1,714 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,396 software developers and data experts.

specialize a template for bitset<N>

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
3 2222
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
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
forward this post to comp.lang.c++.farseers ;-)

Mar 17 '06 #4

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

Similar topics

1
by: Filip Kasperkiewicz | last post by:
Hi ! I need some examples of usage <bitset> class. If you can help me write filipoNO_SPAM@benchmark.pl. Thanks for all help Philip Kasperkiewicz
8
by: Earl Purple | last post by:
On VC++.NET it is implemented like this static int __cdecl compare ( const _Elem *_First1, const _Elem *_First2, size_t _Count ) { // compare [_First1, _First1 + _Count) with [_First2, ...)...
5
by: Artemis Fowl | last post by:
when i used bitset<> to accept data from the user, much to my surprise, i got the wrong answer. After a bit of fiddling, figured out that the data being entered was being consumed in reverse. I...
4
by: lutorm | last post by:
Hi all, I'm having a problem writing template functions that take vector<T>::iterator as arguments and I'm sure you guys can set me straight. Like this: #include<vector> using namespace std; ...
8
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to std::vector<intprogrammed analagously). Is it...
4
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
4
by: mark4asp | last post by:
I have an element, report which contains tags which have been transformed. E.g. <pis &lt;p&gt <myXml> <report>This text has html tags in it.&lt;p&gt which but <has been changed to &lt;&gt</report>...
1
by: VaidehiPawar | last post by:
I am a beginner level in xml..my output page does not convert &gt &lt it shows something like this " &lt;b&gt;Location.&lt;/b&gt;&lt;br /&gt; &lt;UL&gt;&lt;LI&gt;Park Central New York " can anyone help? here is my code ...
4
by: abhinuke | last post by:
So I have been working on this code where I have to partition 8 bits into two set of 4 bits each.So far I have been trying my luck with bitset<> container class in C++.But it doesnt give me any...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.