By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Getting a function pointer to an instance of a template

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.