x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Unreal and Union

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.

Product Version: UE 4.16
Tags:
more ▼

asked Jul 06 '17 at 02:30 PM in C++ Programming

avatar image

Chartas
14 3 5 10

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Jul 06 '17 at 05:24 PM

avatar image

Steve Robb STAFF
2.8k 58 27 99

avatar image Chartas Jul 06 '17 at 11:08 PM

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. =)

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question