# How can I handle std::function being different in Unreal plugin and third party library?

**URL:** <https://forums.unrealengine.com/t/how-can-i-handle-std-function-being-different-in-unreal-plugin-and-third-party-library/455421>\
**Category:** Plugins\
**Tags:** compatibility, unreal-engine, crash, Plugins, CPP, question\
**Created:** [September 16, 2019, 7:07pm UTC](https://forums.unrealengine.com/t/how-can-i-handle-std-function-being-different-in-unreal-plugin-and-third-party-library/455421 "2019-09-16T19:07:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MartinNilsson](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MartinNilsson](https://forums.unrealengine.com/u/MartinNilsson)\
**Post date:** [September 16, 2019, 5:47pm UTC](https://forums.unrealengine.com/t/how-can-i-handle-std-function-being-different-in-unreal-plugin-and-third-party-library/455421/1 "2019-09-16T17:47:59Z")

</div>

I found [Cross-platform library integration in Unreal Engine 4 - Adam Rehn](https://adamrehn.com/articles/cross-platform-library-integration-in-unreal-engine-4/) , [Using bundled libc++ in ThirdParty - C++ - Unreal Engine Forums](https://answers.unrealengine.com/questions/802042/using-bundled-libc-in-thirdparty.html) and [Compiling libraries in Linux - Platform & Builds - Unreal Engine Forums](https://answers.unrealengine.com/questions/674473/compiling-libraries-in-linux.html) . The solution seems to be to build the third-party library with clang and libc++.

---

<div class="post-metadata">

**Author:** ![MartinNilsson](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MartinNilsson](https://forums.unrealengine.com/u/MartinNilsson)\
**Post date:** [September 16, 2019, 7:07pm UTC](https://forums.unrealengine.com/t/how-can-i-handle-std-function-being-different-in-unreal-plugin-and-third-party-library/455421/2 "2019-09-16T19:07:08Z")

</div>

Hi,

I’m trying to use a third party library in UE4 but I’m getting garbage data and crashes. I have determined that the cause of the problem is that the size and alignment requirement of `std::function` differ between code compiled stand-alone and code compiled with the Unreal Build Tool as part of a plugin. In particular, this line produces different output:

`std::cout << sizeof(std::function<void(int*)>) << ' ' << std::alignment_of< std::function<void(int*)> >() << '\n';`

My stand-alone program prints `32 8` while adding this line to an Unreal Engine plugin prints `48 16`. It doesn’t matter if I use `g++` or `clang++` from the system (7.4.0 and 6.0.0, respectively), or if I use the `clang++` provided by Unreal Engine in `UnrealEngine/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v13_clang-7.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang++` (7.0.1) for the stand-alone test.

The effect of this is that a class such as the following from the third party library:

```
template<size_t NUM_BUFFERS>
class CustomContainer
{
  CustomContainer();
  void set(size_t buffer, size_t index, int value)
  {
    m_buffers[buffer][index] = value;
    m_buffer_updated_callback(m_buffers[buffer]);
  }
  
private:
  std::function<void(int*)> m_buffer_updated_callback;
  int* m_buffers[NUM_BUFFERS];
};

```

will access an incorrect element of `m_buffers`, and possibly even read outside the array entirely, when `set` is called from the plugin since the constructor will place the `m_buffers` array at one offset while the inlined `set` method, which contains code compiled by the Unreal Build Tool, will try to access the array at another offset.

What can I do to make the Unreal Build Tool create `std::function` instances that are compatible with the normal/default `std::function`?  
Are there other classes where I can expect the same problem to occur?
