Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12870 > unrolled thread
| Started by | darylew@gmail.com |
|---|---|
| First post | 2011-12-29 18:43 -0800 |
| Last post | 2011-12-29 19:16 -0800 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.c++
Using enable_if and function templates darylew@gmail.com - 2011-12-29 18:43 -0800
Re: Using enable_if and function templates "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-12-30 04:14 +0100
Re: Using enable_if and function templates 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 19:16 -0800
| From | darylew@gmail.com |
|---|---|
| Date | 2011-12-29 18:43 -0800 |
| Subject | Using enable_if and function templates |
| Message-ID | <31202471.738.1325213025379.JavaMail.geo-discussion-forums@vbyc2> |
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.
[toc] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Date | 2011-12-30 04:14 +0100 |
| Message-ID | <jdjaag$m8f$1@dont-email.me> |
| In reply to | #12870 |
On 30.12.2011 03:43, darylew@gmail.com 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
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2011-12-29 19:16 -0800 |
| Message-ID | <16601197.1645.1325214991960.JavaMail.geo-discussion-forums@prez15> |
| In reply to | #12870 |
dar...@gmail.com於 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web