From: Marcus on
Hello all,
Can anyone help me with a particularly obscure error? The following
code fails to compile on g++

template<typename retT, typename argT>
void func(retT (*funcPtr)(argT), argT argument)
{}

void func1(int a) {}
void func2(int& a) {}

int main()
{
int val = 1;
int& valref = val;

func(&func1, valref);//Succeeds
func(&func2, valref);//Fails
}

The compiler appears to lose the ampersand from argT when it creates
the function. I'm completely stuck on this one so any help would be
greatly appreciated.
Thanks,
Marcus

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Johannes Schaub (litb) on
Marcus wrote:

> Hello all,
> Can anyone help me with a particularly obscure error? The following
> code fails to compile on g++
>
> template<typename retT, typename argT>
> void func(retT (*funcPtr)(argT), argT argument)
> {}
>
> void func1(int a) {}
> void func2(int& a) {}
>
> int main()
> {
> int val = 1;
> int& valref = val;
>
> func(&func1, valref);//Succeeds
> func(&func2, valref);//Fails
> }
>
> The compiler appears to lose the ampersand from argT when it creates
> the function. I'm completely stuck on this one so any help would be
> greatly appreciated.

"argT" is deduced to different types in the second function call. It's "int"
for the right function parameter, but it is "int&" for the left function
parameter. Thus you have a contradiction, and type deduction fails.

You are going to need to make the left parameter a non-deduced context so
that it won't participate

template<typename T> struct identity { typedef T type; };

template<typename retT, typename argT>
void func(retT (*funcPtr)(argT), typename identity<argT>::type argument)
{}


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]