C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question on type deduction

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
francis_r
Guest





PostPosted: Wed Jul 12, 2006 1:42 am    Post subject: Question on type deduction Reply with quote



Following very simplified code will illustrate my problem:


void Augment( int& outNumber )
{
outNumber++;
}

template< typename ReturnType, typename Type1 >
ReturnType ExecuteFunction( ReturnType (*inFunction)( Type1 ), Type1
inArg1 )
{
return inFunction( inArg1 );
}

int main()
{
int theNumber = 10;
ExecuteFunction( Augment, theNumber ); // <-- compiler error
ExecuteFunction< void, int& >( Augment, theNumber ); // compiles OK
}

Compiler error goes as follows:
function call
ExecuteFunction({lval} void (int &), {lval} int)' does not match
'ExecuteFunction<...>(__T0 (*)(__T1), __T1)'
on line 435 ExecuteFunction( Augment, theNumber );

It seems that the second argument (theNumber) is not passed by
reference even though the Augment requests int& explicitely.

Is there a way I can rewrite the function ExecuteFunction so that I can
call it without having to explicitely specify it's template parameters?
If so, how?

Thanks in advance for all helpful feedback.

Kind regards,
Francis


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Carl Barron
Guest





PostPosted: Thu Jul 13, 2006 3:29 am    Post subject: Re: Question on type deduction Reply with quote



In article <1152610779.648325.84140 (AT) 75g2000cwc (DOT) googlegroups.com>,
francis_r <francis.rammeloo (AT) gmail (DOT) com> wrote:

Quote:
Following very simplified code will illustrate my problem:


void Augment( int& outNumber )
{
outNumber++;
}

template< typename ReturnType, typename Type1
ReturnType ExecuteFunction( ReturnType (*inFunction)( Type1 ), Type1
inArg1 )
{
return inFunction( inArg1 );
}

Is there a way I can rewrite the function ExecuteFunction so that I can
call it without having to explicitely specify it's template parameters?

template <class R,class T

R exec_function(R (*f)(T &),T &x)
{
return f(x);
}

template <class R,class T>
R exec_function(R (*f)(T),T x)
{
return f(x);
}

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





PostPosted: Thu Jul 13, 2006 3:30 am    Post subject: Re: Question on type deduction Reply with quote



change template function signature

from
------------------------------------------------------------------------------------------------------------
template< typename ReturnType, typename Type1 >
ReturnType ExecuteFunction( ReturnType (*inFunction)( Type1 ), Type1
inArg1 )

to

template< typename ReturnType, typename Type1 >
ReturnType ExecuteFunction( ReturnType (*inFunction)( Type1& ), Type1
inArg1 )

francis_r wrote:
Quote:
Following very simplified code will illustrate my problem:


void Augment( int& outNumber )
{
outNumber++;
}

template< typename ReturnType, typename Type1
ReturnType ExecuteFunction( ReturnType (*inFunction)( Type1 ), Type1
inArg1 )
{
return inFunction( inArg1 );
}

int main()
{
int theNumber = 10;
ExecuteFunction( Augment, theNumber ); // <-- compiler error
ExecuteFunction< void, int& >( Augment, theNumber ); // compiles OK
}

Compiler error goes as follows:
function call
ExecuteFunction({lval} void (int &), {lval} int)' does not match
'ExecuteFunction<...>(__T0 (*)(__T1), __T1)'
on line 435 ExecuteFunction( Augment, theNumber );

It seems that the second argument (theNumber) is not passed by
reference even though the Augment requests int& explicitely.

Is there a way I can rewrite the function ExecuteFunction so that I can
call it without having to explicitely specify it's template parameters?
If so, how?

Thanks in advance for all helpful feedback.

Kind regards,
Francis

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





PostPosted: Thu Jul 13, 2006 3:32 am    Post subject: Re: Question on type deduction Reply with quote

francis_r:
Quote:

void Augment( int& outNumber )
{
outNumber++;
}
int main()
{
int theNumber = 10;
ExecuteFunction( Augment, theNumber ); // <-- compiler error
ExecuteFunction< void, int& >( Augment, theNumber ); // compiles OK
}


void Augment( int* outNumber )
{
(*outNumber)++;
}
int main()
{
int theNumber = 10;
ExecuteFunction( Augment, &theNumber ); // <-- compiler error
ExecuteFunction< void, int* >( Augment, &theNumber ); // compiles
OK
}


[...]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ian Wakeling
Guest





PostPosted: Thu Jul 13, 2006 3:50 am    Post subject: Re: Question on type deduction Reply with quote

francis_r wrote:

Quote:
Following very simplified code will illustrate my problem:


void Augment( int& outNumber )
{
outNumber++;
}

template< typename ReturnType, typename Type1
ReturnType ExecuteFunction( ReturnType (*inFunction)( Type1 ), Type1
inArg1 )
{
return inFunction( inArg1 );
}

int main()
{
int theNumber = 10;
ExecuteFunction( Augment, theNumber ); // <-- compiler error
ExecuteFunction< void, int& >( Augment, theNumber ); // compiles OK
}

Compiler error goes as follows:
function call
ExecuteFunction({lval} void (int &), {lval} int)' does not match
'ExecuteFunction<...>(__T0 (*)(__T1), __T1)'
on line 435 ExecuteFunction( Augment, theNumber );

It seems that the second argument (theNumber) is not passed by
reference even though the Augment requests int& explicitely.

Is there a way I can rewrite the function ExecuteFunction so that I can
call it without having to explicitely specify it's template parameters?
If so, how?

Thanks in advance for all helpful feedback.

Kind regards,
Francis


Look at the types involved in the call that doesn't work:
Quote:
ExecuteFunction( Augment, theNumber ); // <-- compiler error
Augment is a pointer to a function that takes an int& and returns void.

theNumber is an int.

So, the generated instance of ExecuteFunction will look like this:

void ExecuteFunction( void (*inFunction)( int& ), int inArg1 )
{
return inFunction( inArg1 );
}

Hopefully the problem is now obvious: inArg1 is an int, which cannot be
converted to an int& for the call to inFunction.

To be honest, the use of an int& is not really good style anyway: given the
calling site, how is the poor maintenance programmer supposed to know that
calling ExecuteFunction() is going to alter the value of theNumber? Yes,
they can work it out by looking at the definition of Augment, but you are
just setting traps for the unwary, which won't earn you any friends when
the pressure is on to iron out the last few bugs and ship the product.

The good news is that if you remove the use of int&, you not only make the
code more obvious, you solve the compilation problem as well. Either
replace the int& with an int*, so that the calling site becomes

ExecuteFunction( Augment, &theNumber );

or adopt a more value based approach (which will sit better with the STL)
and change Augment to return the altered value instead:

int Augment( int num )
{
return( num + 1 );
}

This also yields a more readable calling site:
int theNumber = 10;
theNumber = ExecuteFunction( Augment, theNumber );

Ian


-- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Przemyslaw Szymanski
Guest





PostPosted: Thu Jul 13, 2006 4:03 am    Post subject: Re: Question on type deduction Reply with quote

francis_r wrote:
Quote:
Following very simplified code will illustrate my problem:


void Augment( int& outNumber )
{
outNumber++;
}

template< typename ReturnType, typename Type1
ReturnType ExecuteFunction( ReturnType (*inFunction)( Type1 ), Type1
inArg1 )
{
return inFunction( inArg1 );
}

int main()
{
int theNumber = 10;
ExecuteFunction( Augment, theNumber ); // <-- compiler error
ExecuteFunction< void, int& >( Augment, theNumber ); // compiles OK
}

Compiler error goes as follows:
function call
ExecuteFunction({lval} void (int &), {lval} int)' does not match
'ExecuteFunction<...>(__T0 (*)(__T1), __T1)'
on line 435 ExecuteFunction( Augment, theNumber );

It seems that the second argument (theNumber) is not passed by
reference even though the Augment requests int& explicitely.

Is there a way I can rewrite the function ExecuteFunction so that I can
call it without having to explicitely specify it's template parameters?
If so, how?

The problem is that argument deduction mechanism is trying to deduce
the actual type of Type1 template parameter from both actual parameters
of ExecuteFunction instantiation. From the first argument it is 'int &'
but from the second it's 'int', so the deduction fails. You could try
to introduce a nondeducible context for the second parameter which will
limit deduction to the first argument only:

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

template< typename ReturnType, typename Type1 >
ReturnType ExecuteFunction( ReturnType (*inFunction)( Type1 ), typename
wrapper<Type1>::type inArg1 )
{
return inFunction( inArg1 );
}

Regards,
Przemek.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.