Unreal and Unions

Short Version:

Why does the following ClassWithUnion compile in a clean c++ project, but not if I use it in unreal?

class ClassWithUnion

		new (&data) DataClass();


		DataClass data;

I get the following error upon compilation:
error C4582: ‘ClassWithUnion::data’: constructor is not implicitly called

No, I cannot avoid using these constructs.

Long Version:
The error is kind of correct because it’s being explicilty called… But it is valid C++ to do it like this, which I confirmed by creating an empty project with just that class. That one does compile without problems and the expected behavior.

I’m trying to use the OpenSource CActorFramework, which is making extensive use of anonymous unions for varius purposes. Mainly to avoid calling constructors in cases where it is not required or would lead to invalid behavior if they were called. So sadly not using those unions would pretty much mean not using the Framework at all or having to rewrite large parts of the framework, which would kind of defeat the purpose of using it in the first place.

I’m on Windows 7 and tried compiling with Visual Studio 2015 and 2017 but neither changed the result.

The main goal of using the Framework was to use it for large scale background simulation of an abstract game world. Actors that would leave the sphere around the player would be simulated using the more efficent and slimed down message passing interface implemented by the CAF. This would not happen in real time but instead discrete time steps for various buckets of the world. Actors close to the player sphere would then be simulated in real time by UE4. Sadly since i can’t get it to compile together with UE4 that’s apparently not going to happen anytime soon.

I’ve found that hidding CAF behind a dll and then linking that into the project worked, but that heavly limits the usefullnes and requires me to duplicate, (or even triple) certain classes because i can’t directly use them due to having to hide the CAF definitions. Also base types like FVector would have to be reimplemented within the dll requiring additional work. (And more duplicates…)

So if you have either an idea why that compiler error happens in ue4 or an alternative open source framework for simulating my actors in the background, that would be much appreciated.

The way I see it, you have a few options:

  1. Make sure all unions are using simple value/POD types or pointers (there are tons of unions within the engine, the problem here is the type contained by your union and how you’re initializing it). If you change your union to DataClass* data and then just use the normal new/free - it’ll work fine.
  2. If you want to keep that setup, then you’ll need to compile this code in a separate lib/dll and link it in which will avoid all the custom settings that UE4 does during compilation (UHT, compile flags, etc).
  3. Abandon using CAF and instead write the features you need as needed.

Anytime you try and link another lib into UE4 - you’re rolling the dice. UE4 is very particular in how it compiles and makes various assumptions around constructors and other common C++ constructs.

Just to clarify something, I’m not trying to inhert from one of those union classes and then make it into a UCLASS or something. I’m only trying to use them as member variables or even just use them within function bodies.

What I suspect is happening, is that somewhere a flag is passed to the compiler resulting in compiling in a c++ language version, that does not yet support the syntax for this. I tried looking at VCToolchain.cs in the UnrealBuildTool but couldn’t find anything. If I could find the right place for it I would like to try passing the nmake \std:c++latest flag, but I’ve got not clue how to go about that.

Concerning your solutions, sadly the first is not applicable and the other two would mean multiple month of work and more. =( I can’t be the first to try something like simulating a background world through something that uses an actor model. Does somebody know of some alternative frameworks, maybe?

Also thanks for the fast reply.

Thanks to Steves reply I got the solution. =)

Apparently the problem is that UE4 enables additonal warnings and also treats warnings as errors. So the solution is disabling the two specific warnings causing problems here:

#pragma warning(disable : 4582)
#pragma warning(disable : 4583)