From -1722621854648965416 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,5f0d50dba412123c X-Google-Attributes: gidf78e5,public X-Google-ArrivalTime: 2001-02-15 15:22:04 PST Path: supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!195.64.68.27!newsgate.cistron.nl!bullseye.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail From: Martin Sebor Newsgroups: comp.std.c++ Subject: Re: Template function overloading: must every candidate functions be instanciated? Date: Thu, 15 Feb 2001 23:21:28 GMT Organization: Rogue Wave Software, Inc. Approved: Fergus Henderson , moderator of comp.std.c++ Message-ID: <3A8C503F.8D510ABC@roguewave.com> References: <3A8AB2A5.79CFB902@meteo.fr> X-Trace: mail2news.demon.co.uk 982279294 mail2news:19450 mail2news mail2news.demon.co.uk X-Complaints-To: abuse@demon.net X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov) X-Authentication-Warning: backdraft.briar.org: smap set sender to using -f Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Date: 15 Feb 2001 22:52:17 GMT X-Accept-Language: en Lines: 120 Xref: supernews.google.com comp.std.c++:3647 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::epsilon())" line > in the first appearing definition of sinc_pi. I believe the program you give below is ill-formed. Specifically, the part > template class U> > inline T sinc_pi(const U 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" (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 U> inline U sinc_pi(const U &x) { if (abs (x) <= std::numeric_limits::epsilon()) return T(1); return sin (x) / x; } > > The message error in itself is reasonable, as numeric_limits is not > instanciated for ::std::complex, and the compiler is trying to > instanciate "complex sinc_pi >(const > complex 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 > #include > #include > > /* This is the "Sinus Cardinal" of index Pi*/ > > template > inline T sinc_pi(const T x) > { > using ::std::abs; > using ::std::sin; > > if (abs(x) <= ::std::numeric_limits::epsilon()) > { > return(T(1)); > } > else > { > return(sin(x)/x); > } > } > > template class U> > inline T sinc_pi(const U x) > { > using ::std::abs; > using ::std::sin; > > if ((abs(x) <= ::std::numeric_limits::epsilon()) > { > return(T(1)); > } > else > { > return(sin(x)/x); > } > } > > /*** ***/ > /*** The Problem ***/ > /*** ***/ > > int main() > > { > ::std::complex 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. ]