### 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:

 ``123456789101112`` ``````// Fundamental object structure template struct myInt { myInt() { value = T; }; int value; }; // Operation - Addition template 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.
 ``123456789`` ``````template 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:

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include 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 > { 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::value << " sum: " << sum::value << '\n' ; using nine_to_fifteen = push_front< int, 9, ten_to_fifteen >::type ; std::cout << "size of nine_to_fifteen: " << size::value << " sum: " << sum::value << '\n' ; using eleven_to_fifteen = pop_front< ten_to_fifteen >::type ; std::cout << "size of eleven_to_fifteen: " << size::value << " sum: " << sum::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:

 ``12345`` `````` typedef seq_c c; typedef seq_c X; typedef seq_c Y; typedef seq_c Z; typedef seq_c 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...

 ``12`` `````` Coord 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.