473,320 Members | 2,109 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,320 software developers and data experts.

Getting a function pointer to an instance of a template

Pan
I was going to make many similar functions,
So I have wondered whether I use templates or macros.
Since I should use function pointers of each function, I tried to
compile the code below to test it, but an error occured.

#include <stdio.h>

#define make_print_macro(T) \
void print_macro_##T( T a ) \
{ \
printf( "%d\n", a); \
}

make_print_macro(int);

#define print_macro(T) print_macro_##T

template<class T>
void print_tmplt( T a )
{
printf ( "%d\n", a );
}

int main()
{
print_macro(int)( 10 );
print_tmplt<int>( 10 );
printf ( "%p\n", (void *)(print_macro(int)) );
//printf ( "%p\n", (void *)(print_tmplt<int>) ); // THIS CODE
return 0;
}

All works fine except the line I marked 'THIS CODE'.

error: cannot resolve overloaded function `print_tmplt' based on
conversion to type `void*'

Can't I obtain a function pointer to an instance of a template so that
I must use macro in this case?
Jul 23 '05 #1
1 1690
Pan wrote:
I was going to make many similar functions,
So I have wondered whether I use templates or macros.
Since I should use function pointers of each function, I tried to
compile the code below to test it, but an error occured.

#include <stdio.h>

#define make_print_macro(T) \
void print_macro_##T( T a ) \
{ \
printf( "%d\n", a); \
}

make_print_macro(int);
Drop the semicolon here.

#define print_macro(T) print_macro_##T

template<class T>
void print_tmplt( T a )
{
printf ( "%d\n", a );
}

int main()
{
print_macro(int)( 10 );
print_tmplt<int>( 10 );
printf ( "%p\n", (void *)(print_macro(int)) );
//printf ( "%p\n", (void *)(print_tmplt<int>) ); // THIS CODE
Remove both casts to void* so you have

printf ( "%p\n", print_macro(int) );
printf ( "%p\n", print_tmplt<int> ); // THIS CODE
return 0;
}

All works fine except the line I marked 'THIS CODE'.

error: cannot resolve overloaded function `print_tmplt' based on
conversion to type `void*'

Can't I obtain a function pointer to an instance of a template so that
I must use macro in this case?


The code compiles fine with Comeau, with Intel C/C++ v8, after my
corrections. Visual C++ up to version 8 Beta fails to compile it.
I guess it's a bug in VC++. What compiler are you using?

V
Jul 23 '05 #2

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

Similar topics

15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
4
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
4
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
5
by: asianmuscle | last post by:
I am trying to learn RAII and some template techniques by writer a smarter pointer class to manage the resource management. Since I find that a lot of the resource management is kinda the same, I...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
6
by: chris.kemmerer | last post by:
I am having a problem with templates and I hope someone here can help. I am writing a library that accepts data packets, parses them and saves the information for later use. One member of the...
2
by: webinfinite | last post by:
This is my first post here, please tell me if I did anything wrong. in the following code snippet: 1. template <class In, class Pred> 2. In find_if(In begin, In end, Pred f) { 3. while (begin...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.