Connecting Tech Pros Worldwide Help | Site Map

int template argument

  #1  
Old March 18th, 2008, 06:26 PM
Hicham Mouline
Guest
 
Posts: n/a
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,


  #2  
Old March 18th, 2008, 07:25 PM
Christian Hackl
Guest
 
Posts: n/a

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
  #3  
Old March 18th, 2008, 07:35 PM
jason.cipriani@gmail.com
Guest
 
Posts: n/a

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
  #4  
Old March 18th, 2008, 08:15 PM
Kai-Uwe Bux
Guest
 
Posts: n/a

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
  #5  
Old March 18th, 2008, 11:45 PM
Greg Herlihy
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert template argument to string Marco Nef answers 9 October 17th, 2008 08:25 AM
"could not deduce template argument" error gretean@comcast.net answers 7 November 10th, 2006 02:45 PM
template template argument Fei Liu answers 4 August 3rd, 2006 04:35 PM
Template argument deduction and conversion operators. BigMan@abv.bg answers 3 July 23rd, 2005 01:26 AM