Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using enable_if and function templates

Reply
Thread Tools

Using enable_if and function templates

 
 
darylew@gmail.com
Guest
Posts: n/a
 
      12-30-2011
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)[S], T const (&r)[S] )
{
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-30-2011
On 30.12.2011 03:43, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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)[S], T const (&r)[S] )
> {
> 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
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      12-30-2011
(E-Mail 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)[S], T const (&r)[S] )
> {
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
restrict_to, enable_if, etc greek_bill C++ 3 10-08-2008 07:13 PM
boost::enable_if related code fails to compile on VC++8.0 Philipp Reh C++ 2 03-19-2007 08:56 PM
boost::enable_if and constructors Christoph Bartoschek C++ 3 12-04-2006 12:18 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments