HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Integer packs (and integer pack utilities) for template meta-programming

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
templatepackspackprogrammingmetaforutilitiesandinteger

Problem

Introduction

In template meta-programming, integer sequences and ranges are very useful. I've made a couple of utility classes that do various operations on compile-time integer packs.

The implementation is non-recursive (except for \$log(n)\$ recursions when generating a pack), allowing faster compilation and a greater number of template arguments.

Note 1: In every implementation section, at the top of every namespace, there's a comment indicating which feature is inside.

Note 2: Include guards are omitted.

Note 3: The following headers are used:

#include 
#include 
#include 
#include 


Part 1

Part 1 consists of the basic operations:

  • Integer packs themselves.



  • Getting the size of an integer pack.



  • Creating sequences and ranges.



Overview

  • integer_pack



A pack of integers of type T.

  • index_pack



An alias of integer_pack.

  • integer_pack_size



Provides the number of integers in the specified template argument integer pack.

  • make_integer_sequence



Makes an integer_pack type where T... is the values in the range \$[ 0, n - 1 )\$ when \$n >= 0\$. The template performs \$log(n)\$ recursions to generate the sequence.

  • make_integer_range



Make an integer_pack type where T... is the values in the range \$[from, to]\$ using an increment/decrement of step. step will be subtracted if \$from > to\$.

Tests

```
// integer_pack_test_make_sequence_and_make_range
namespace ct
{
namespace test
{
template
constexpr void integer_pack_test() noexcept
{
static_assert(std::is_same::value);
}

constexpr void integer_pack_test_make_sequence_and_make_range() noexcept
{
// make sequence
using make_seq5_t = make_integer_sequence;
using expected_make_seq5_t = integer_pack;
integer_pack_test();

// make sequence 0 : make empty sequence
using make_seq0_t = make_integer_sequence;
using expec

Solution

First of all, your code is very impressive. I was going through it to improve my template programming skills when I saw a potential issue.

The following test code works when char is signed type.

using make_range_3_to_neg9 = make_integer_range;
using expected_make_range_3_to_neg9 = integer_pack;
integer_pack_test();


But it fails at compile time with static_assert if you use unsigned char. If char happens to be an unsigned type on a platform, you will run into the same problem.

I would disable integer_pack since char is not guaranteed to be a signed type.

template
struct integer_pack
{
    static_assert(false, "integer_pack: Disabled for char");
};

Code Snippets

using make_range_3_to_neg9 = make_integer_range<char, 3, -9, 3>;
using expected_make_range_3_to_neg9 = integer_pack<char, 3, 0, -3, -6, -9>;
integer_pack_test<make_range_3_to_neg9, expected_make_range_3_to_neg9>();
template<char... ints>
struct integer_pack<char, ints...>
{
    static_assert(false, "integer_pack: Disabled for char");
};

Context

StackExchange Code Review Q#133626, answer score: 2

Revisions (0)

No revisions yet.