Josh
Josh

Reputation: 295

Is there a valid way to use number values in templates?

I have the situation where I need to split a number of bitset blocks in half. Is there a way to merge the following two functions into something resembling the third? Having something like the third feels more right and general.

pair<bitset<32>, bitset<32> > 
split_block_in_half(
  bitset<64> block
){
  bitset<block.size()> filter;
  for(size_t i = 0; i < filter.size()/2; ++i){
    filter.set(i);
  }

  return make_pair<bitset<block.size()/2>, bitset<block.size()/2> >(
    bitset<block.size()/2>(((block ^ (filter << (block.size()/2))) >> (block.size()/2)).to_ulong()),
    bitset<block.size()/2>((block ^ filter).to_ulong())
  );
}


pair<bitset<28>, bitset<28> > 
split_block_in_half(
  bitset<56> block
){
  bitset<block.size()> filter;
  for(size_t i = 0; i < filter.size()/2; ++i){
    filter.set(i);
  }

  return make_pair<bitset<block.size()/2>, bitset<block.size()/2> >(
bitset<block.size()/2>(((block ^ (filter << (block.size()/2))) >> (block.size()/2)).to_ulong()),
bitset<block.size()/2>((block ^ filter).to_ulong())
  );
}



template<
  typename N>
pair<bitset<N>, bitset<N> >
split_block_in_half(
  bitset<2*N> block
){
  bitset<block.size()> filter;
  for(size_t i = 0; i < filter.size()/2; ++i){
    filter.set(i);
  }

  return make_pair<bitset<block.size()/2>, bitset<block.size()/2> >(
    bitset<block.size()/2>(((block ^ (filter << (block.size()/2))) >> (block.size()/2)).to_ulong()),
    bitset<block.size()/2>((block ^ filter).to_ulong())
  );
}

Is there a clever compile time way to merge the two separate functions above, or is this unsupported?

Upvotes: 2

Views: 108

Answers (2)

iBug
iBug

Reputation: 37247

Try an integer value in template parameter:

template <int N>
pair<bitset<N>, bitset<N>> 
split_block_in_half(
    bitset<2*N> block
)
......

The downside is that, since 2*N cannot be deduced from function calls, you need to explicitly specify N:

split_block_in_half<28>(block); // where block is bitset<56>

If you define it like this, you can use SFINAE to restrict that N be even.

template <int N>
std::enable_if_t<
    N % 2 == 0,
    pair<bitset<N/2>, bitset<N/2>>
>
split_block_in_half(bitset<N>);

If you don't have C++14, change std::enable_if_t<> to typename std::enable_if<>::type (C++11).

Upvotes: 3

alter_igel
alter_igel

Reputation: 7212

Here's a full working example that doesn't require specifying N. I implemented the logic a bit differently. I assume the goal is to have the pair's first member contain the high order bits and have second contain the low order bits. Do be aware that ^ is the logical XOR operation, though it may resemble the logical AND sometimes used in propositional logic.

template<size_t N>
pair<bitset<N/2>, bitset<N/2>> split_block_in_half(const bitset<N>& block){
    static_assert(N % 2 == 0 && N != 0, "N must be even and non-zero");
    pair<bitset<N/2>, bitset<N/2>> ret;
    for (size_t i = 0; i < N/2; ++i){
        ret.first[i] = block[i + N/2];
        ret.second[i] = block[i];
    }
    return ret;
}

int main() {

    bitset<64> bs { 0x1234567887654321 };

    auto halves = split_block_in_half(bs);

    std::cout << std::hex
        << halves.first.to_ullong() << '\n'
        << halves.second.to_ullong() << '\n';

    // prints:
    // > 12345678
    // > 87654321

    return 0;
}

Upvotes: 2

Related Questions