more documentation about callback errors
authorTom Henderson <tomh@tomh.org>
Mon, 10 May 2010 21:10:15 -0700
changeset 6295 347c3b3a1394
parent 6294 6cefb3c0696a
child 6296 b2b5d61f5946
more documentation about callback errors
doc/manual/callbacks.texi
samples/main-callback.cc
--- a/doc/manual/callbacks.texi	Mon May 10 16:41:20 2010 +0200
+++ b/doc/manual/callbacks.texi	Mon May 10 21:10:15 2010 -0700
@@ -380,8 +380,12 @@
 So in the above example, we have a declared a callback named "one" that will
 eventually hold a function pointer.  The signature of the function that it will
 hold must return double and must support two double arguments.  If one tries 
-to pass a function whose signature does not match the declared callback, a
-run-time NS_FATAL_ERROR will be raised.
+to pass a function whose signature does not match the declared callback, 
+a compilation error will occur.  Also, if one tries to assign to a callback
+an incompatible one, compilation will succeed but a run-time 
+NS_FATAL_ERROR will be raised.  The sample program 
+@command{samples/main-callback.cc} demonstrates both of these error cases
+at the end of the @command{main()} program.
 
 Now, we need to tie together this callback instance and the actual target function
 (CbOne).  Notice above that CbOne has the same function signature types as the 
--- a/samples/main-callback.cc	Mon May 10 16:41:20 2010 +0200
+++ b/samples/main-callback.cc	Mon May 10 21:10:15 2010 -0700
@@ -54,5 +54,19 @@
   //int retTwoNull = two (20.0);
   NS_ASSERT (two.IsNull ());
 
+#if 0
+  // The below type mismatch between CbOne() and callback two will fail to 
+  // compile if enabled in this program.
+  two = MakeCallback (&CbOne);
+#endif
+
+#if 0
+  // This is a slightly different example, in which the code will compile
+  // but because callbacks are type-safe, will cause a fatal error at runtime 
+  // (the difference here is that Assign() is called instead of operator=)
+  Callback<void, float> three;
+  three.Assign (MakeCallback (&CbOne));
+#endif
+
   return 0;
 }