Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12872
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Using enable_if and function templates |
| Date | 2011-12-29 19:16 -0800 |
| Organization | http://groups.google.com |
| Message-ID | <16601197.1645.1325214991960.JavaMail.geo-discussion-forums@prez15> (permalink) |
| References | <31202471.738.1325213025379.JavaMail.geo-discussion-forums@vbyc2> |
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.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web