Using enable_if and function templates

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.
 
A

Alf P. Steinbach

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,


Why have you not tested that before posting?

What does your compiler say?

Does it say it works, or does it say it does not work?

can it also work on operator templates?

An operator is just a way of denoting a function.

Anyway, don't use enable_if where simple specialization or simple
runtime checking is more appropriate (as it is here), and don't reinvent
the wheel when the standard library already offers the desired
functionality (as it does here).

I.e., do use std::lexicographical_compare.


Cheers & hth.,

- Alf
 
8

88888 Dihedral

(e-mail address removed)æ–¼ 2011å¹´12月30日星期五UTC+8上åˆ10時43分45秒寫é“:
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 )


The pointer ll won't be overflow. The length par of S is passed by ????

If S is fixed, this is just trivial in the compiler.





{
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.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top