Template function as argument to another template function

Discussion in 'C++' started by Jim West, Oct 7, 2004.

  1. Jim West

    Jim West Guest

    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);
    }


    The Intel compiler gives

    $ icc simple.cc
    asdf.cc(9): error: no instance of function template "BAR" matches the
    argument list
    argument types are: (<unknown-type>, double)
    double y = BAR(FOO, x);
    ^
    compilation aborted for simple.cc (code 2)
    $
     
    Jim West, Oct 7, 2004
    #1
    1. Advertising

  2. Jim West <> 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.

    Unfortunately I can't tell you what the standard says to such things.

    Kind regrads,
    Nicolas

    --
    | Nicolas Pavlidis | Elvis Presly: |\ |__ |
    | Student of SE & KM | "Into the goto" | \|__| |
    | | ICQ #320057056 | |
    |-------------------University of Technology, Graz----------------|
     
    Nicolas Pavlidis, Oct 7, 2004
    #2
    1. Advertising

  3. Nicolas Pavlidis <> writes:

    > Jim West <> 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" | \|__| |
    | | ICQ #320057056 | |
    |-------------------University of Technology, Graz----------------|
     
    Nicolas Pavlidis, Oct 7, 2004
    #3
  4. Jim West

    Jim West Guest

    On 2004-10-07, Jim West <> wrote:
    >
    > 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);
    > }
    >
    >
    > The Intel compiler gives
    >
    > $ icc simple.cc
    > asdf.cc(9): error: no instance of function template "BAR" matches the
    > argument list
    > argument types are: (<unknown-type>, double)
    > double y = BAR(FOO, x);
    > ^
    > compilation aborted for simple.cc (code 2)
    > $



    Well, I did some deeper digging on Google, and about 7 pages into
    my search I found

    <http://groups.google.com/groups?hl=en&lr=&threadm=c9kp5e%24r5j%241%40news.wplus.net&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26selm%3Dc9kp5e%2524r5j%25241%2540news.wplus.net>

    which gave me the "dont_deduce" solution:

    template <typename T>
    struct dont_deduce {
    typedef T type;
    };

    template <typename T>
    T FOO(const T &a) { return a; }

    template <typename T>
    T BAR(typename dont_deduce<T (*)(const T &b)>::type f, const T &c)
    {
    return f(c);
    }

    int main() {
    double x = 10.0;
    double y = BAR(FOO, x);
    }

    This works on both compilers. According to the thread I linked, the
    Intel compiler is probably following the standard more accurately.

    All answers lie with Google if you just dig in deeply enough!

    Thanks to those who responded.
     
    Jim West, Oct 7, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Kial
    Replies:
    1
    Views:
    666
    Eric Enright
    Nov 15, 2004
  2. Vijai Kalyan
    Replies:
    4
    Views:
    714
    Vijai Kalyan
    Nov 8, 2005
  3. nw
    Replies:
    0
    Views:
    316
  4. Reckoner
    Replies:
    11
    Views:
    708
    Steven D'Aprano
    Jan 19, 2009
  5. AikidoGuy
    Replies:
    11
    Views:
    561
    Seebs
    Nov 21, 2011
Loading...

Share This Page