title: Replace function-pointer implementation with std::function for using lambdas in PlayFab SDK
body: <p>I'm currently trying to include the <a href="https://api.playfab.com/sdks/cocos2d-x" rel="nofollow">PlayFab C++ SDK</a> into my app. This SDK is mainly for the game engine Cocos2d-x, but can be used for any C++ app basically.</p>
  
  <p>It's just plain REST, hence you send requests to their servers and wait for the response. This would be perfect for using lambdas.</p>
  
  <p>They declare this callback, which is called when a request is successful:</p>
  
  <pre><code>template&lt;typename ResType&gt; using ProcessApiCallback = void(*)(const ResType&amp; result, void* userData);
  </code></pre>
  
  <p>Unfortunately, they're not using std::function, but a function pointer. This way one can not use lambdas that capture variables.</p>
  
  <p>Hence, I thought I could simply replace this function pointer callback with an std::function callback like so:</p>
  
  <pre><code>template&lt;typename ResType&gt; using ProcessApiCallback = std::function&lt;void(const ResType&amp; result, void* userData)&gt;;
  </code></pre>
  
  <p>Unfortunately, things are not that simply, as they stored the function pointers using ugly reinterpret_casts, here's an example (remove unnecessary parts to keep it short):</p>
  
  <pre><code>void PlayFabClientAPI::LoginWithAndroidDeviceID(
      LoginWithAndroidDeviceIDRequest&amp; request,
      ProcessApiCallback&lt;LoginResult&gt; callback,
      ErrorCallback errorCallback,
      void* userData
      )
  {
      // here they assign the callback to the httpRequest
      httpRequest-&gt;SetResultCallback(reinterpret_cast&lt;void*&gt;(callback));
      httpRequest-&gt;SetErrorCallback(errorCallback);
      httpRequest-&gt;SetUserData(userData);
  
      PlayFabSettings::httpRequester-&gt;AddRequest(httpRequest, OnLoginWithAndroidDeviceIDResult, userData);
  }
  </code></pre>
  
  <p>Later on, when the request was successful, they do this:</p>
  
  <pre><code>if (request-&gt;GetResultCallback() != nullptr)
  {
      ProcessApiCallback&lt;LoginResult&gt; successCallback = reinterpret_cast&lt;ProcessApiCallback&lt;LoginResult&gt;&gt;(request-&gt;GetResultCallback());
      successCallback(outResult, request-&gt;GetUserData());
  }
  </code></pre>
  
  <p>The HttpRequest class has this field:</p>
  
  <pre><code>void* mResultCallback;
  </code></pre>
  
  <p>The problem is that I don't know how to store arbitrary std::function pointers in the HttpRequest class and then later cast them back. I tried many things, including also really ugly reinterpret_casting, but nothing did work.</p>
  
  <p>I'm open to do any changes to their SDK. I also reported this as bad design and they agreed, but they don't have the time to improve it, but they will accept pull request if a good solution can be found.</p>