473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Template function as argument to another template function


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)
$
Jul 22 '05 #1
3 1564
Jim West <eg***********@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.

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" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #2
Nicolas Pavlidis <pa****@sbox.tugraz.at> writes:
Jim West <eg***********@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" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #3
On 2004-10-07, Jim West <eg***********@yahoo.com> 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.

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short,...
3
by: CoolPint | last post by:
Can anyone explain how I can make the following function accept an default arguement for the last parameter, which should be an optional functor? template <typename T, typename FUNCTOR> void...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
5
by: Steve | last post by:
Hi, Does C++ allow the programmer to declare a template with in a template so that a generic function can instantiate the embedded template? For example, could code such as this exist: ...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
5
by: dilip ranganathan | last post by:
Hi I have taken the liberty to cross-post this. It appeared on c.l.c++.m but the ICE is regarding VS.NET 7.1 C++ compiler. post follows: ==============================================...
1
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
5
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; //...
5
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.