Topic: Template function overloading: must every candidate functions be


Author: Martin Sebor <sebor@roguewave.com>
Date: Thu, 15 Feb 2001 23:21:28 GMT
Raw View
Hubert HOLIN wrote:
>
> Somewhere in the E.U., le 14/02/2001
>
>         Considering the following complete code, my compiler (MetroWerks
> CodeWarrior Pro 6) indicates a compile error (illegal operand) on line
> 15, that's the "if      (abs(x) <= ::std::numeric_limits<T>::epsilon())" line
> in the first appearing definition of sinc_pi.

I believe the program you give below is ill-formed. Specifically, the part

> template<template<typename T> class U>
> inline T        sinc_pi(const U<T> x)
> {

is wrong, since, if I'm reading 14.1, p13 correctly, the scope of T ends at the
end of U.

In any case, you wouldn't want to (or couldn't) return T from the function but
rather "U<T>" (but you can't do that since T is not in scope). I don't see why
you'd want to use template template parameters here, but if you must this would
compile (I have no idea whether it does what you want, though :)

template<class T, template<typename> class U>
inline U<T> sinc_pi(const U<T> &x)
{
    if (abs (x) <= std::numeric_limits<T>::epsilon())
        return T(1);
    return sin (x) / x;
}


>
>         The message error in itself is reasonable, as numeric_limits is not
> instanciated for ::std::complex<float>, and the compiler is trying to
> instanciate "complex<float> sinc_pi<complex<float> >(const
> complex<float> x)".
>
>         However, I wonder if it is necessary to actually instanciate this
> function. It is necessary to *consider* this function for the overload
> resolution, but the second function should be preferred as it is more
> specialized (though the differing return type makes that statement
> slightly dubious, at least to me), so actually instanciating the first
> function seems unnecessary.

Candidate function templates are not instantiated during overload resolution.
Only the best viable function may eventually be instantiated.

>
>         In short, is the following code legal, or not:

Nope.

Regards
Martin

>
>  --- 8>< --------------------------- ><8 ---
> #include <cmath>
> #include <limits>
> #include <complex>
>
> /* This is the "Sinus Cardinal" of index Pi*/
>
> template<typename T>
> inline T        sinc_pi(const T x)
> {
>         using   ::std::abs;
>         using   ::std::sin;
>
>         if      (abs(x) <= ::std::numeric_limits<T>::epsilon())
>         {
>                 return(T(1));
>         }
>         else
>         {
>                 return(sin(x)/x);
>         }
> }
>
> template<template<typename T> class U>
> inline T        sinc_pi(const U<T> x)
> {
>         using   ::std::abs;
>         using   ::std::sin;
>
>         if      ((abs(x) <= ::std::numeric_limits<T>::epsilon())
>         {
>                 return(T(1));
>         }
>         else
>         {
>                 return(sin(x)/x);
>         }
> }
>
> /***                                    ***/
> /***    The Problem             ***/
> /***                                    ***/
>
> int     main()
>
> {
>         ::std::complex<float>   c(1,2);
>
>         sinc_pi(c);
> }
>  --- 8>< --------------------------- ><8 ---
>
>                 Hubert Holin
>                 Hubert.Holin@Bigfoot.com
>                 Hubert.Holin@meteo.fr

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Hubert HOLIN <Hubert.Holin@meteo.fr>
Date: Wed, 14 Feb 2001 19:34:33 GMT
Raw View
Somewhere in the E.U., le 14/02/2001

 Considering the following complete code, my compiler (MetroWerks
CodeWarrior Pro 6) indicates a compile error (illegal operand) on line
15, that's the "if (abs(x) <= ::std::numeric_limits<T>::epsilon())" line
in the first appearing definition of sinc_pi.

 The message error in itself is reasonable, as numeric_limits is not
instanciated for ::std::complex<float>, and the compiler is trying to
instanciate "complex<float> sinc_pi<complex<float> >(const
complex<float> x)".

 However, I wonder if it is necessary to actually instanciate this
function. It is necessary to *consider* this function for the overload
resolution, but the second function should be preferred as it is more
specialized (though the differing return type makes that statement
slightly dubious, at least to me), so actually instanciating the first
function seems unnecessary.

 In short, is the following code legal, or not:


 --- 8>< --------------------------- ><8 ---
#include <cmath>
#include <limits>
#include <complex>


/* This is the "Sinus Cardinal" of index Pi*/

template<typename T>
inline T sinc_pi(const T x)
{
 using ::std::abs;
 using ::std::sin;

 if (abs(x) <= ::std::numeric_limits<T>::epsilon())
 {
  return(T(1));
 }
 else
 {
  return(sin(x)/x);
 }
}

template<template<typename T> class U>
inline T sinc_pi(const U<T> x)
{
 using ::std::abs;
 using ::std::sin;

 if ((abs(x) <= ::std::numeric_limits<T>::epsilon())
 {
  return(T(1));
 }
 else
 {
  return(sin(x)/x);
 }
}


/***     ***/
/*** The Problem  ***/
/***     ***/

int main()

{
 ::std::complex<float> c(1,2);

 sinc_pi(c);
}
 --- 8>< --------------------------- ><8 ---

  Hubert Holin
  Hubert.Holin@Bigfoot.com
  Hubert.Holin@meteo.fr

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]