[Boost.Function] loosely enforced function signature

AMDG Slawomir Lisznianski wrote:
Given these two type declarations:
typedef boost::function1<void, int &> signature;
typedef void (*fp)(int &);
and these two function definitions:
void not_match(int v) { } // arg by value
void match(int & v) { } // arg by ref
The following seems to be true on my compiler (GCC 4.0.2):
Case 1) signature fn = match; // compiles
Case 2) signature fn = not_match; // compiles (problematic)
Case 3) fp fn = match; // compiles
Case 4) fp fn = not_match; // fails
My question is, shouldn't Boost.Function library fail in case 2 with the error along the lines of case 4's: "error: invalid conversion from ?void (*)(int)? to ?void (*)(int&)?"?
I noticed this problem when users of my library register callbacks but occasionally mistype their function signature. They end up modifying function-local, rather than "out", variables.
This behavior is intended. http://www.boost.org/doc/html/function/reference.html#function.definitions This particular mistake could be made to cause an error by changing the definition of compatibility to: / // if ResultType is not *void*/ ResultType foo(Arg1 arg1, Arg2 arg2, ..., Arg/N/ arg/N/) { *return* f((Arg1)arg1, (Arg2)arg2, ..., (ArgN)arg/N/); } /// if ResultType is *void*/ ResultType foo(Arg1 arg1, Arg2 arg2, ..., Arg/N/ arg/N/) { f((Arg1)arg1, (Arg2)arg2, ..., (ArgN)arg/N/); } concept_check can be used to avoid actually making copies at runtime. In Christ, Steven Watanabe
participants (1)
-
Steven Watanabe