How to accumulate template parameter pack?

idclev 463035818

Suppose I want to enable writing this:

template <int a,int b> struct add_base{ static const int value = a+b;};
template<int...a> using add = accumulate<add_base,0,a...>;

template <int a,int b> struct mult_base{ static const int value = a*b;};
template<int...a> using mult = accumulate<mult_base,1,a...>;

template <int a,int b> struct sqsum_base{ static const int value = a+b*b;};
template<int...a> using sqsum = accumulate<sqsum_base,0,a...>;

static_assert( add<1,2,3>::value == 6 );
static_assert( mult<2,2,2>::value == 8 );
static_assert( sqsum<1,2,3>::value == 14 );

My accumulate looks like this:

template <template <int,int> class G,
          int first, int second,
          int...more> 
struct accumulate {
    static const int value = accumulate<G,G<first,second>::value,more...>::value;
};
template <template <int,int> class G,
          int first, int second> 
struct accumulate<G,first,second> {
    static const int value = G<first,second>::value;
};

Now I wonder if accumulate can be condensed by expanding the recusion inline, something like:

template <template <int,int> class G,
          int first,int second,
          int...more> 
struct accumulate {
        static const int value = G< G<first,second>::value , more...>::value;
};

This is wrong and will result in

error: Wrong number of template arguments (3 should be 2)

Is it possible to unpack the parameters to instantiate G recursively in one line? If not, how to write accumulate without having to write a specialization?

user7860670

If your compiler has support for C++17 then you may want to utilize fold expression:

template<int ... x_item> struct
accumulate
{
    static inline constexpr int const s_value{(0 + ... + x_item)};
};

static_assert(6 == accumulate<1, 2, 3>::s_value);

online compiler

Example of parametrized operation:

template<typename x_Op, int ... x_items> struct
accumulate
{
    static inline constexpr int const s_value{(x_Op{0} + ... + x_Op{x_items}).value};
};

struct
sq_sum
{
    int value;
};

inline constexpr sq_sum
operator +(sq_sum left, sq_sum right)
{
    return sq_sum{left.value + right.value * right.value};
}

static_assert(14 == accumulate<sq_sum, 1, 2, 3>::s_value);

online compiler

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is a parameter pack a template parameter?

How to "duplicate" template parameter pack expansion?

How to fill array with contents of a template parameter pack?

How to use enable if with template arguments and parameter pack?

How to specify a default argument for a template parameter pack?

double template parameter pack

aliassing a template parameter pack

Aliasing a template parameter pack

template parameter pack in using

How do I get access to template parameters of a template pack parameter

How to use and access a template parameter pack of parameter packs

Template Meta Programming: How to combine parameter packs to new parameter pack

How to extract a value from a variadic template parameter pack by index?

Template Parameter Pack: How to create a Tuple of an independent type with same length

How can a template parameter pack have both explicit and deduced arguments?

How can I access the types in a C++ template parameter pack?

How can a type be removed from a template parameter pack?

How can a template parameter pack have other trailing arguments?

How implement SFINAE limited to few types, using template parameter pack

How do I retrieve a template parameter pack given a std::variant?

How to call function that use part of template parameter pack in c++?

Idiomatic template parameter pack wrapper for template parameter pack deduction disambiguation

incorrect deduction of template parameter pack

Inspect template parameter pack in gdb

Referring to template parameter pack in class

Instantiating a parameter pack of template parameters

Factor a template parameter pack operation

Expand parameter pack for a function template

Dynamically build a template parameter pack

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive