Unreal and Union

Thank you very much. =) I was not aware those were actually warnings. By using

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

right before my include of the CAF header, I was able to compile succesfully. And by enabling the warning right after the include again I don’t even lose out on the safety in my own code. =)

Short Version:

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

class ClassWithUnion
{
public:

	ClassWithUnion()
	{
		new (&data) DataClass();
	}

	~ClassWithUnion()
	{
		data.~DataClass();
	}

	union
	{
		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.

Hello,

Presumably the problem is that VC doesn’t enable that warning by default, and UE4 does, and also enables warnings-as-errors. VC is correct to warn, IMHO, so we wouldn’t want to disable that warning. I would recommend writing your class like this:

class ClassWithUnion
{
public:

    ClassWithUnion()
    {
        new (&data) DataClass();
    }

    ~ClassWithUnion()
    {
        ((DataClass*)&data)->~DataClass();
    }

    union
    {
        TTypeCompatibleBytes<DataClass> data;
    };
};

This makes it clear that you don’t want an instance of DataClass (which cannot be properly constructed, because it’s in a union), but rather a blob of memory into which you construct a DataClass.

Alternatively, you can disable the warning number in your copy of WindowsPlatformCompilerSetup.h, but you will need to keep this up to date whenever you update your engine version.

Hope this helps,

Steve