specialize a template for bitset<N>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shaun

    #1

    specialize a template for bitset<N>

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

    template<typena me 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
  • Gernot Frisch

    #2
    Re: specialize a template for bitset&lt;N&gt;

    [color=blue]
    > where line 21 is the first line of my function specialized for
    > bitset.[/color]

    post this line. It's got a bug/typo.


    Comment

    • Victor Bazarov

      #3
      Re: specialize a template for bitset&lt;N&gt;

      shaun wrote:[color=blue]
      > I have a function for returning the value of a bit field in a number:
      >
      > template<typena me 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?[/color]

      '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

      Comment

      • Diego Martins

        #4
        Re: specialize a template for bitset&lt;N&gt;

        forward this post to comp.lang.c++.f arseers ;-)

        Comment

        Working...