Using enable_if and function templates

Discussion in 'C++' started by darylew@gmail.com, Dec 30, 2011.

  1. Guest

    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.
     
    , Dec 30, 2011
    #1
    1. Advertising

  2. On 30.12.2011 03:43, 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), 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
     
    Alf P. Steinbach, Dec 30, 2011
    #2
    1. Advertising

  3. æ–¼ 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.
     
    88888 Dihedral, Dec 30, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. recover
    Replies:
    2
    Views:
    819
    recover
    Jul 25, 2006
  2. Christoph Bartoschek

    boost::enable_if and constructors

    Christoph Bartoschek, Dec 3, 2006, in forum: C++
    Replies:
    3
    Views:
    787
    David Harmon
    Dec 4, 2006
  3. Philipp Reh
    Replies:
    2
    Views:
    741
    Philipp Reh
    Mar 19, 2007
  4. greek_bill

    restrict_to, enable_if, etc

    greek_bill, Oct 6, 2008, in forum: C++
    Replies:
    3
    Views:
    365
    Gennaro Prota
    Oct 8, 2008
  5. Replies:
    5
    Views:
    169
    Öö Tiib
    Jun 15, 2013
Loading...

Share This Page