How can I perform arithmetic operations with template parameters?

Hello all,

I'm seeking help with how to use template parameters to perform arithmetic operations on objects.

I feel that it would best to demonstrate my issue rather than try and explain it.

Sample:

1
2
3
4
5
6
7
8
9
10
11
12
// Fundamental object structure
template<int T> struct myInt
{
    myInt() { value = T; };
    int value;
};

// Operation - Addition
template<class T1, class T2> struct add
{
    // Add integral value 'T' from both myInt
};


What I don't know is how to get a hold of the T variable to add them through the 'add' structure. Also, might any of this have to do with sequence wrappers?
I've been reading a little online about them, and I haven't been able to figure out how to create one.

seq_c<T,c1,c2,... cn> is essentially what I'm thinking of. Where T in this case is the type and c to the nth c are the values.

Thank you.
This is how you can do it.
1
2
3
4
5
6
7
8
9
template<class T1, class T2> struct add
{
    add() {
        value = T1.value;
        value1 = T2.value;
        value += value1;
    }
    int value, value1;
};
Last edited on
> seq_c<T,c1,c2,... cn> is essentially what I'm thinking of.
> Where T in this case is the type and c to the nth c are the values.

If you want to implement it as an academic exercise, something like this:

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
#include <iostream>

template< typename T, T... VALUES  > struct seq_c {};

template< typename T, T... > struct size ;
template< typename T, T... VALUES > struct size< seq_c< T, VALUES...> >
{ enum { value = sizeof...(VALUES) } ; } ;

template< typename T, T V, typename U > struct push_front {} ;
template< typename T, T V, T... VALUES > struct push_front< T, V, seq_c< T, VALUES...> >
{ typedef seq_c< T, V, VALUES... > type ; } ;

template< typename T > struct pop_front {} ;
template< typename T, T V, T... VALUES > struct pop_front< seq_c< T, V, VALUES...> >
{ typedef seq_c< T, VALUES...> type ; } ;


template< typename T > struct sum ;
template< typename T > struct sum < seq_c<T> > { static constexpr T value = T() ; } ;
template< typename T, T V, T... VALUES > struct sum< seq_c< T, V, VALUES...> >
{ static constexpr T value = V + sum< seq_c< T, VALUES... > >::value ; } ;

// .. etc

int main()
{
    using ten_to_fifteen = seq_c< int, 10, 11, 12, 13, 14, 15 > ;
    std::cout << "size of ten_to_fifteen: " << size<ten_to_fifteen>::value
              << "    sum: " << sum<ten_to_fifteen>::value << '\n' ;

    using nine_to_fifteen = push_front< int, 9, ten_to_fifteen >::type ;
    std::cout << "size of nine_to_fifteen: " << size<nine_to_fifteen>::value
              << "    sum: " << sum<nine_to_fifteen>::value << '\n' ;

    using eleven_to_fifteen = pop_front< ten_to_fifteen >::type ;
    std::cout << "size of eleven_to_fifteen: " << size<eleven_to_fifteen>::value
              << "    sum: " << sum<eleven_to_fifteen>::value << '\n' ;
}

http://ideone.com/fnZvQz

For production code, use boost::mpl. For instance boost::mpl::vector_c<>
http://www.boost.org/doc/libs/1_54_0/libs/mpl/doc/refmanual/vector-c.html
Zoran, my only concern with that is that it seems very specialized. Suppose there is no member 'value' of an particular class?

JLBorges: Good find! I have been looking for something like that to help me along with developing a metafunction library. Thank you very much. My next step is:

1
2
3
4
5
   typedef seq_c<int, 0, 0, 0> c;
   typedef seq_c<int, 1, 0, 0> X;
   typedef seq_c<int, 0, 1, 0> Y;
   typedef seq_c<int, 0, 0, 1> Z;
   typedef seq_c<int, 1, 1, 1> Pt;


Where I can change the values using add, sub, mlt, and div arithmetic functions to make types like:

XY --- seq_c<int, 1, 1, 0>
YZ --- seq_c<int, 0, 1, 1>
XZ --- seq_c<int, 1, 0, 1>

How can I change these values to create new types using arithmetic metafunctions?

and manipulate values...

1
2
   Coord<int, X> myX[3] = { 5, 10, 0 };
   myX[2] = myX[0] + myX[1];


where myX[2] now equals 15.

> How can I change these values to create new types using arithmetic metafunctions?

Have a look at the metafunctions in boost::mpl.
http://www.boost.org/doc/libs/1_54_0/libs/mpl/doc/refmanual/metafunctions.html

That would give you pointers to how you could implement your own metafunctions.
Topic archived. No new replies allowed.