diff --git a/api/GenApi.py b/api/GenApi.py
index 1a733014e67e83dcad638475a7e3828f0ad0dca8..f17cb3987e2776054f2d1b89f26bed6853fc9922 100644
--- a/api/GenApi.py
+++ b/api/GenApi.py
@@ -738,7 +738,14 @@ def isizefun(name):
     a.cpp = "std::function<double(int, int, double, double, double)> " + name
     a.c_arg = "std::bind(" + name + ", std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, " + name + "_data)"
     a.c = "double (*" + name + ")(int dim, int tag, double x, double y, double z, void * data), void * " + name + "_data"
-    a.cwrap_arg = a.c_arg
+    a.cwrap_pre = "struct " + name + """_caller_  {
+          static double call(int entity_dim, int entity_tag, double x, double y, double z, void *callbackp) {
+            return (*static_cast<std::function<double(int,int,double,double,double)>*> (callbackp))(entity_dim, entity_tag, x, y, z);
+          }
+        };
+        auto *""" + name + "_ptr_ = new std::function<double(int,int,double,double,double)>("+name+""");
+"""
+    a.cwrap_arg = "&" + name + "_caller_::call, "+name+"_ptr_"
     a.python_pre = ("global api_" + name + "_type_\n" +
                     "            api_" + name + "_type_ = " +
                     "CFUNCTYPE(c_double, c_int, c_int, c_double, c_double, c_double)\n" +
diff --git a/api/gmsh.h b/api/gmsh.h
index 9c934c6ca86bced603bc5b2b1ce742be60b060f3..f348df1b6ce87a91caed45ccdf97a0e47e731d02 100644
--- a/api/gmsh.h
+++ b/api/gmsh.h
@@ -1175,7 +1175,7 @@ namespace gmsh { // Top-level functions
       // Set a global mesh size callback. The callback should take 5 arguments
       // (`dim', `tag', `x', `y' and `z') and return the value of the mesh size at
       // coordinates (`x', `y', `z').
-      GMSH_API void setSizeCallback(std::function<double(int,int,double,double,double)> callback);
+      GMSH_API void setSizeCallback(std::function<double(int, int, double, double, double)> callback);
 
       // gmsh::model::mesh::removeSizeCallback
       //
diff --git a/api/gmsh.h_cwrap b/api/gmsh.h_cwrap
index 20ce4e18545f734c0fa00450ce37a1aac3e8c167..70cf2198d72cf9c7ac52e961f8466aa7ebe1f5cf 100644
--- a/api/gmsh.h_cwrap
+++ b/api/gmsh.h_cwrap
@@ -1788,10 +1788,16 @@ namespace gmsh { // Top-level functions
       // Set a global mesh size callback. The callback should take 5 arguments
       // (`dim', `tag', `x', `y' and `z') and return the value of the mesh size at
       // coordinates (`x', `y', `z').
-      inline void setSizeCallback(std::function<double(int,int,double,double,double)> callback)
+      inline void setSizeCallback(std::function<double(int, int, double, double, double)> callback)
       {
         int ierr = 0;
-        gmshModelMeshSetSizeCallback(std::bind(callback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, callback_data), &ierr);
+        struct callback_caller_  {
+          static double call(int entity_dim, int entity_tag, double x, double y, double z, void *callbackp) {
+            return (*static_cast<std::function<double(int,int,double,double,double)>*> (callbackp))(entity_dim, entity_tag, x, y, z);
+          }
+        };
+        auto *callback_ptr_ = new std::function<double(int,int,double,double,double)>(callback);
+        gmshModelMeshSetSizeCallback(&callback_caller_::call, callback_ptr_, &ierr);
         if(ierr) throwLastError();
       }
 
diff --git a/api/gmshc.cpp b/api/gmshc.cpp
index b1bbde07d1622e5caa0d17f8cd4e379e6846bc19..873baf36f3999586807ff6410ea7e02f6f82bb51 100644
--- a/api/gmshc.cpp
+++ b/api/gmshc.cpp
@@ -1508,7 +1508,7 @@ GMSH_API void gmshModelMeshSetSizeAtParametricPoints(const int dim, const int ta
   }
 }
 
-GMSH_API void gmshModelMeshSetSizeCallback(double (*callback)(int dim, int tag, double x, double y, double z, void *data), void *callback_data, int * ierr)
+GMSH_API void gmshModelMeshSetSizeCallback(double (*callback)(int dim, int tag, double x, double y, double z, void * data), void * callback_data, int * ierr)
 {
   if(ierr) *ierr = 0;
   try {
diff --git a/api/gmshc.h b/api/gmshc.h
index a29f3824d28fb3ee49c6528aec1f4f787bf82387..5f3afcbcc1c0c65cc8552cafbcae36ea52fcf147 100644
--- a/api/gmshc.h
+++ b/api/gmshc.h
@@ -1033,7 +1033,7 @@ GMSH_API void gmshModelMeshSetSizeAtParametricPoints(const int dim,
 /* Set a global mesh size callback. The callback should take 5 arguments
  * (`dim', `tag', `x', `y' and `z') and return the value of the mesh size at
  * coordinates (`x', `y', `z'). */
-GMSH_API void gmshModelMeshSetSizeCallback(double (*callback)(int dim, int tag, double x, double y, double z, void *data), void *callback_data,
+GMSH_API void gmshModelMeshSetSizeCallback(double (*callback)(int dim, int tag, double x, double y, double z, void * data), void * callback_data,
                                            int * ierr);
 
 /* Remove the global mesh size callback. */