Template function as argument to another template function

J

Jim West

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)
$
 
N

Nicolas Pavlidis

Jim West said:
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
 
N

Nicolas Pavlidis

Nicolas Pavlidis said:
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
 
J

Jim West

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=&selm=c9kp5e%24r5j%241%40news.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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top