Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template function as argument to another template function

Reply
Thread Tools

Template function as argument to another template function

 
 
Jim West
Guest
Posts: n/a
 
      10-07-2004

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)
$
 
Reply With Quote
 
 
 
 
Nicolas Pavlidis
Guest
Posts: n/a
 
      10-07-2004
Jim West <(E-Mail Removed)> 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" | \|__| |
| http://www.velocityreviews.com/forums/(E-Mail Removed) | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
 
Reply With Quote
 
 
 
 
Nicolas Pavlidis
Guest
Posts: n/a
 
      10-07-2004
Nicolas Pavlidis <(E-Mail Removed)> writes:

> Jim West <(E-Mail Removed)> 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" | \|__| |
| (E-Mail Removed) | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
 
Reply With Quote
 
Jim West
Guest
Posts: n/a
 
      10-07-2004
On 2004-10-07, Jim West <(E-Mail Removed)> 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.w plus.net&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26selm%3Dc9kp5e%2524r5j%2 5241%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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointer as template argument - does not work for function template avasilev C++ 4 12-22-2011 02:11 PM
function argument dependent on another function argument? Reckoner Python 11 01-19-2009 03:31 AM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM



Advertisments