int template argument

H

Hicham Mouline

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,
 
J

jason.cipriani

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]

Well, you do know that the thing the compiler is warning you about
isn't going to cause any problems; you could consider turning the
warning off, or leaving a comment that says "the warning on this line
means nothing". Many compilers have a way to turn off specific
warnings for small portions of code, usually in the form of a #pragma
directive.
Is there a way to make the compiler ignore the non-binomial code ?

Assuming that explicit specialization of your entire function is not
acceptable (because you have a lot of other common code between the
two cases), your only other option AFAIK is to separate that part of
the code into a different function. You can use explicit
specialization of the other function, something like:

===== BEGIN CODE =====

#include <iostream>

using namespace std;

template <int n> void FillArray (double *);

template<> void FillArray<2> (double *array) {

array[0] = 1.0;
array[1] = 2.0;

}

template<> void FillArray<3> (double *array) {

array[0] = 4.0;
array[1] = 5.0;
array[2] = 6.0;

}

template <typename cT, typename tT, int n>
void FillAndPrintArray () {

double array[n];

// do things specific to array size
FillArray<n>(array);

// common code
cout << n << ": " << endl;
for (int i = 0; i < n; ++ i)
cout << array << endl;

}

int main (int, char **) {

FillAndPrintArray<int,int,2>();
FillAndPrintArray<int,int,3>();

return 0;

}

===== END CODE =====


There, FillArray has explicit specialization for the case of n = 2 and
n = 3. The correct one is called from FillAndPrintArray.

Note that without a default implementation of FillArray (as in the
above example), if you try calling FillAndPrintArray<...,...,4>() you
will get a linker error.


Jason
 
C

Christian Hackl

Hicham said:
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
 
J

jason.cipriani

Christian Hackl said:
Hicham said:
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
 
K

Kai-Uwe Bux

Hicham said:
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
 
P

peter koch

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,

Do as Kai-Uwe suggests. I'll just suggest that you google for SFINAE C+
+ (SFINAE: Substitiution Failure Is Not An Error) in case you need
more inspiration.

/Peter
 
G

Greg Herlihy

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
 
J

James Kanze

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?
Do as Kai-Uwe suggests. I'll just suggest that you google for
SFINAE C+ + (SFINAE: Substitiution Failure Is Not An Error) in
case you need more inspiration.

I'm not sure that SFINAE is the best solution here. What he's
really looking for is partial specialization, I think. Except,
of course, that partial specialization only works for class
templates, not for function templates. But it shouldn't be too
hard to write his function something like:

template< typename cT, typename tT, int n >
...
{
HelperClass< cT, tT, n > functionalObject ;
// use functionalObject for the actual code...
}

and then use partial specialization for HelperClass. (Kai-Uwe
mentionned overloading, but I'm not sure that's relevant
either.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,499
Latest member
DewittK739

Latest Threads

Top