Connecting Tech Pros Worldwide Help | Site Map

int template argument

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 18th, 2008, 05:26 PM
Hicham Mouline
Guest
 
Posts: n/a
Default int template argument

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, 06:25 PM
Christian Hackl
Guest
 
Posts: n/a
Default 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, 06:35 PM
jason.cipriani@gmail.com
Guest
 
Posts: n/a
Default 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, 07:15 PM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default 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, 10:45 PM
Greg Herlihy
Guest
 
Posts: n/a
Default 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



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.