Should a template function taking pointer to other functions respectdefault values ?

S

samjam86

If default values are to be considered, the below code should compile.
But it is not compiling on g++ 4.4.3.

#include <iostream>

using namespace std;

template<typename FunctionType>
void callback_helper(FunctionType function)
{
function();
}

void print_arg_0()
{
cout << "Inside print_arg_0" << endl;
}

void print_arg_1_opt(int a = 10)
{
cout << "Inside print_arg_1_opt" << endl;
}

int main()
{
callback_helper(print_arg_0);
callback_helper(print_arg_1_opt);
return 0;
}
 
S

samjam86

Your second template instantiation gets expanded to, what essentially is,
the following code:

void callback_helper(void (*function)(int))
{
         function();

}

This, obviously, will not be accepted by any compiler.

The fact that you're passing a pointer to a function whose int argument has
a default value is irrelevant, as far as invoking a function through a
function pointer is concerned. The pointer passed to this function can be
any function pointer, to any function that takes an int argument. When
invoking a function through a function pointer, there is no concept of a
default argument value.

 application_pgp-signature_part
< 1KViewDownload

Well, I can't remember the version of g++ which compiled this code,
but there was at one time. So was that some kind of bug ?
 
S

subramanian100in

OP's program x.cpp:

#include <iostream>

using namespace std;

template<typename FunctionType>
void callback_helper(FunctionType function)
{
function();

}

void print_arg_0()
{
cout << "Inside print_arg_0" << endl;

}

void print_arg_1_opt(int a = 10)
{
cout << "Inside print_arg_1_opt" << endl;

}

int main()
{
callback_helper(print_arg_0);
callback_helper(print_arg_1_opt);
return 0;
}

I am using g++ 3.4.3

The OP's program compiles fine with the options

g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

and produces the output

Inside print_arg_0
Inside print_arg_1_opt

Did I miss out any compilation options ? Or is it a bug with g++ 3.4.3
to have compiled this program without any errors ?

Kindly clarify.

Thanks
V.Subramanian
 
S

sankalp srivastava

Your second template instantiation gets expanded to, what essentially is,
the following code:

void callback_helper(void (*function)(int))
{
         function();

}

This, obviously, will not be accepted by any compiler.

The fact that you're passing a pointer to a function whose int argument has
a default value is irrelevant, as far as invoking a function through a
function pointer is concerned. The pointer passed to this function can be
any function pointer, to any function that takes an int argument. When
invoking a function through a function pointer, there is no concept of a
default argument value.

 application_pgp-signature_part
< 1KViewDownload
hey , you need to refer to some good book ..a pointer to a function
can only be assigned from a wider or equal type to a narrow type ..
The function print , is a narrower function than it's pointer
*function . This pointer cannot accept the same
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top