STL within headers of third party library in UE5

Hello ! I have been reading some posts regarding the use of STL vector and other containers and I have seen that it is generally unsafe to use them inside UE5.

However, if you where to use a third party library inside UE5 that does NOT return any STL containers, would it be safe if those containers do exist in the headers that you must include ?

For example:

class MyClass {
public:
    std::string GetBiggestString();
private:
    std::vector<std::string> strings;
};

I was also wondering if using other containers, like std::unordered_map is considered safe or unsafe. What about std::string or std::array ? I believe it might be just STL containers that use an Allocator (?)

Furthermore, if the third party library would return an actual std::vector would it be safe to create a function that transforms std::vector into TArray without doing any Allocations.

Thanks !

1 Like

Hi, I’m wondering this myself. I think I have an answer though.

By my understanding of C++, as soon as a class works with an STL/std object in any way, even holding it as a private field, then it’s very important to ensure every bit of compiled code which involves that class at all is using the exact same version of the standard library. The best way to achieve this is to compile all code in your project with the same compiler, compiler version, and compiler settings.

Unreal is presumably compiling all modules the same way, and certainly with the same compiler, so within an Unreal project this should not be a problem. However, if a third-party library uses the STL and hands you a pre-compiled library (DLL or LIB file) with headers, then you will most likely have problems using this library.

Side node, one way to sidestep this arduous requirement would be for the library to store the STL object as a void* field, allocating and deallocating it entirely within its own DLL. This design pattern is known as Pointer to Implementation, or PImpl.