D
darylew
Let's say we have a function template like:
template < typename T, unsigned M, unsigned N >
int array_compare( T const (&l)[M], T const (&r)[N] ); // -1, 0 , or +1
where if the lengths are unequal, consider the missing values of the shorter object as zero. Can we use enable_if and function template default-parameters to segregate the code for M > N, M == N, and M < N?
template < typename T, unsigned M, unsigned N, typename DoIt = enable_if<(M > N)>::type >
int array_compare( T const (&l)[M], T const (&r)[N] )
{
// compare l[0..N-1] to r[0..N-1]
// if result_so_far == 0, return comparing l[N..M-1] positively
}
template < typename T, unsigned S >
int array_compare( T const (&l), T const (&r) )
{
for ( T const *ll = l, *rr = r ; ll < l + S ; ++ll, ++rr )
{
if ( *ll > *rr ) return +1;
if ( *ll < *rr ) return -1;
}
return 0;
}
template < typename T, unsigned M, unsigned N, typename DoIt = enable_if<(M < N)>::type >
int array_compare( T const (&l)[M], T const (&r)[N] )
{
// compare l[0..M-1] to r[0..M-1]
// if result_so_far == 0, return comparing r[M..N-1] negatively
}
If this works, can it also work on operator templates?
Daryle W.
template < typename T, unsigned M, unsigned N >
int array_compare( T const (&l)[M], T const (&r)[N] ); // -1, 0 , or +1
where if the lengths are unequal, consider the missing values of the shorter object as zero. Can we use enable_if and function template default-parameters to segregate the code for M > N, M == N, and M < N?
template < typename T, unsigned M, unsigned N, typename DoIt = enable_if<(M > N)>::type >
int array_compare( T const (&l)[M], T const (&r)[N] )
{
// compare l[0..N-1] to r[0..N-1]
// if result_so_far == 0, return comparing l[N..M-1] positively
}
template < typename T, unsigned S >
int array_compare( T const (&l)
{
for ( T const *ll = l, *rr = r ; ll < l + S ; ++ll, ++rr )
{
if ( *ll > *rr ) return +1;
if ( *ll < *rr ) return -1;
}
return 0;
}
template < typename T, unsigned M, unsigned N, typename DoIt = enable_if<(M < N)>::type >
int array_compare( T const (&l)[M], T const (&r)[N] )
{
// compare l[0..M-1] to r[0..M-1]
// if result_so_far == 0, return comparing r[M..N-1] negatively
}
If this works, can it also work on operator templates?
Daryle W.