summaryrefslogtreecommitdiff
path: root/deps/tao_tuple/28626e99/include/tao/seq/sum.hpp
blob: 67ac6715825cbb640abee8d7cdb898888ff37ae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// The Art of C++ / Sequences
// Copyright (c) 2015-2016 Daniel Frey

#ifndef TAOCPP_SEQUENCES_INCLUDE_SUM_HPP
#define TAOCPP_SEQUENCES_INCLUDE_SUM_HPP

#include <type_traits>
#include <utility>

#include "integer_sequence.hpp"
#include "config.hpp"

#ifndef TAOCPP_FOLD_EXPRESSIONS
#include <cstddef>
#include "make_integer_sequence.hpp"
#endif

namespace tao
{
  namespace seq
  {

#ifdef TAOCPP_FOLD_EXPRESSIONS

    template< typename T, T... Ns >
    struct sum
      : std::integral_constant< T, ( Ns + ... + T( 0 ) ) >
    {};

#else

    namespace impl
    {
      template< std::size_t, std::size_t N >
      struct chars
      {
        char dummy[ N + 1 ];
      };

      template< typename, std::size_t... >
      struct collector;

      template< std::size_t... Is, std::size_t... Ns >
      struct collector< index_sequence< Is... >, Ns... >
        : chars< Is, Ns >...
      {};

      template< std::size_t N, typename T, T... Ns >
      struct sum
      {
        using type = std::integral_constant<
          T,
          T( sizeof( collector< make_index_sequence< N >, ( ( Ns > 0 ) ? Ns : 0 )... > ) - N ) -
          T( sizeof( collector< make_index_sequence< N >, ( ( Ns < 0 ) ? -Ns : 0 )... > ) - N ) >;
      };
    }

    template< typename T, T... Ns >
    struct sum
      : impl::sum< sizeof...( Ns ) + 1, T, Ns..., 0 >::type
    {};

#endif

    template< typename T, T... Ns >
    struct sum< integer_sequence< T, Ns... > >
      : sum< T, Ns... >
    {};
  }
}

#endif // TAOCPP_SEQUENCES_INCLUDE_SUM_HPP