Re: Template function as argument to another template function

From: Nicolas Pavlidis (pavnic_at_sbox.tugraz.at)
Date: 10/07/04


Date: 07 Oct 2004 19:38:57 +0200

Nicolas Pavlidis <pavnic@sbox.tugraz.at> writes:

> Jim West <eggplantparts@yahoo.com> writes:
>
> > The following code compiles and executes properly on one compiler
> > (GNU C++) but not on another (Intel C++ V8.1). Which compiler is
> > correct? If my code is improper (that is, the Intel compiler is
> > correct), is there an appropriate way to do what I'm trying to do?
> >
> >
> > template <class T>
> > T FOO(const T &a) { return a; }
> >
> > template <class T>
> > T BAR(T (*f)(const T &b), const T &c) { return f(c); }
> >
> > int main() {
> > double x = 10.0;
> > double y = BAR(FOO, x);
> > }
>
> IMHO intel is "more correct", you want to call BAR this way:
>
> double y = BAR(FOO<double>, x);
>
> Then intel will must not make any problems.
>
> As you see intel needs a real instantiation of the templated function,
> maybe g++ finds the correct type by itself.

g++ is a bit more inteligent :-). It makes the folowing (I think that it
soes after analysing your code) :

The jole is that every instantiation of foo may fit the requirement for
the functionpoiunter in the paramlist for bar, so this is no problem for
g++, the real instatiation of foo is generated while instatiating bar in
main().

intel needs a real function for passing a functionpointer, so the
exlpicit instantiation is necessary, which is a problem for the MSVC -
compilers :-).

Kind regrads,
Nicolas

-- 
|     Nicolas Pavlidis       |       Elvis Presly:    |\ |__      |
|   Student of SE  & KM      |      "Into the goto"   | \|__|     |
|  pavnic@sbox.tugraz.at     |       ICQ #320057056      |        |
|-------------------University of Technology, Graz----------------|


Relevant Pages

  • Re: generics in Ada 83
    ... > The benefit is that if a generic compiles, and the instantiation ... If the template compiles, ... Supplying contracts in C++ would be vastly more difficult than it ... because C++ templates can be specialized whereas Ada generics cannot ...
    (comp.lang.ada)
  • Re: Access Violation error while using pointers
    ... This code compiles and executes perfectly in Boreland C++. ... A string literal is itself an object, and its type is "array of const char" ...
    (microsoft.public.vc.debugger)
  • Re: Ridiculous size of executable? [OT}
    ... >Al puzzuoli wrote: ... >> The code compiles and runs, but I noticed that the size of ... <snip code> ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Class templates and friend function templates
    ... likely it's a VC bug that this code compiles. ... Your code can be made to compile on Comeau if you make the friend ...
    (microsoft.public.vc.language)
  • Re: Extending discriminant types
    ... It's interesting that this compiles, since N is not an integer type. ... PACKAGE uu IS NEW u; ... And then, in 12.3 on Generic Instantiation, ... For a name that denotes a generic formal type: ...
    (comp.lang.ada)