boolean operators for bitsets

How can I write a template for boolean operators for bitsets?

I want to be able to do something like:
1
2
template <class itemtype>
bool operator < (bitset<itemtype> lhs, bitset<itemtype> rhs);


so that I won't have to write a seperate function for each size of bitset.

Thanks
Last edited on
bump
Does the code you posted not work or something?

personally I would pass by const reference:

1
2
template <class T>
bool operator < (const bitset<T>& lhs, const bitset<T>& rhs);

Last edited on
No, not on codeblocks with mingw. I forget the error though (I'm on a different computer right now) but I think it may have something to do with the fact that the parameter for bitset takes a const int, like:bitset<4> B;
Oh. Yeah. And you want to compare bitsets that have a different size?

Then you probably want this:

1
2
template <unsigned A,unsigned B>
bool operator < (const bitset<A>& lhs, const bitset<B>& rhs);

No, just want to compare bitsets with the same size, but I will try that and see if it works.
Oh okay. If you only want the same size, then you can do this:

1
2
template <unsigned A>
bool operator < (const bitset<A>& lhs, const bitset<A>& rhs);
Thanks with your help i was able to come up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<unsigned A>
bool operator < (bitset <A> &lhs, bitset <A> &rhs)
{
    for (int i = A-1; i >=0; --i)
        if (lhs[i] < rhs[i])
            return true;
    return false;
}

template<unsigned A, unsigned B>
bool operator < (bitset <A> & lhs, bitset <B> & rhs)
{
    if (A < B)
        return true;
    else return false;
}


Maybe I should add a cerr << message to the latter.

Thanks.
Topic archived. No new replies allowed.