Connecting Tech Pros Worldwide Forums | Help | Site Map

int template argument

Hicham Mouline
Guest
 
Posts: n/a
#1: Mar 18 '08
hi,

I have a template function <typename cT, typename tT, int n>
{
.....
double array[n];

if (n==2)
binomial code
else if (n==3)
trinomial code (array[0] array[1] array[2])
else
.....


compiler tries to compile the "trinomial code" code even when the template
is instantiated with n=2,
and that gives warnings because of array[2]

Is there a way to make the compiler ignore the non-binomial code ?
some way with boost perhaps?

rds,



Christian Hackl
Guest
 
Posts: n/a
#2: Mar 18 '08

re: int template argument


Hicham Mouline wrote:
Quote:
I have a template function <typename cT, typename tT, int n>
{
....
double array[n];
>
if (n==2)
binomial code
else if (n==3)
trinomial code (array[0] array[1] array[2])
else
....
>
>
compiler tries to compile the "trinomial code" code even when the template
is instantiated with n=2,
and that gives warnings because of array[2]
>
Is there a way to make the compiler ignore the non-binomial code ?
some way with boost perhaps?
There's boost::array, which you could instead of the naked array.

http://www.boost.org/doc/html/array.html



--
Christian Hackl
jason.cipriani@gmail.com
Guest
 
Posts: n/a
#3: Mar 18 '08

re: int template argument


"Christian Hackl" <hacki@sbox.tugraz.atwrote in message news:frp17l
$kfv$1@aioe.org...
Quote:
Hicham Mouline wrote:
>
Quote:
>I have a template function <typename cT, typename tT, int n>
>{
>....
>double array[n];
>>
>if (n==2)
> binomial code
>else if (n==3)
> trinomial code (array[0] array[1] array[2])
>else
>....
>>
>>
>compiler tries to compile the "trinomial code" code even when the template
>is instantiated with n=2,
>and that gives warnings because of array[2]
>>
>Is there a way to make the compiler ignore the non-binomial code ?
>some way with boost perhaps?
>
There's boost::array, which you could instead of the naked array.
>
http://www.boost.org/doc/html/array.html
If the boost array is just a wrapper for a constant-sized array,
wouldn't a compiler that generates warnings about out-of-bounds
accesses complain when compiling the boost array template code, too?
Or is it just hidden behind a function parameter, in which case the
following would also kill the compiler warning in the original code:

double & access_array (double *a, int n) {
return a[n];
}

And then instead if:

....
else if (n==3) {
array[0] ...
array[1] ...
array[2] ...
}
....

It becomes:

....
else if (n==3) {
access_array(array, 0) ...
access_array(array, 1) ...
access_array(array, 2) ...
}
....

Jason
Kai-Uwe Bux
Guest
 
Posts: n/a
#4: Mar 18 '08

re: int template argument


Hicham Mouline wrote:
Quote:
hi,
>
I have a template function <typename cT, typename tT, int n>
{
....
double array[n];
>
if (n==2)
binomial code
else if (n==3)
trinomial code (array[0] array[1] array[2])
else
....
You should overload the function for different values of n instead of using
an if statement to distinguish the cases. Alternatively, you can isolate
that piece of code into another function and overload that. Something like:

template < int n, typename cT, typename tT >
void polynomial_code ( ct & c, tT & t );

template < typename cT, typename tT>
void polynomial_code<2,cT,tT( ct & c, tT & t ) {
binomial code
}

template < typename cT, typename tT>
void polynomial_code<3,cT,tT( ct & c, tT & t ) {
trinomial code
}


template <typename cT, typename tT, int n>
...
polynomial_code<n>( ct_arg, tt_arg );
...

Maybe, I got the syntax slightly wrong, but I hope the idea is clear.


[snip]

Best

Kai-Uwe Bux
Greg Herlihy
Guest
 
Posts: n/a
#5: Mar 18 '08

re: int template argument


On Mar 18, 10:26*am, "Hicham Mouline" <hic...@mouline.orgwrote:
Quote:
I have a template function <typename cT, typename tT, int n>
{
....
double array[n];
>
if (n==2)
* binomial code
else if (n==3)
* trinomial code *(array[0] array[1] array[2])
else
Have you tried:

double array[n];

if (n==2)
binomial code
else if (n==3)
trinomial code (array[0] array[1] array[n-1])

Greg



Closed Thread