Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unreal Engine calls these macros...is this normal in C++ now?

Tags:

c++

I used to be a C++ programmer and to me a macro was a preprocessor definition using#define.

Now I am getting back into programming with Unreal engine which uses C++ but there are all these macros UCLASS() UFUNCTION() FORCELINE that the UNreal tutorial calls a macro. I've never seen anything like this before and would like to understand it.

I am not asking what the macro does in Unreal, but for someone to help me fill in my knowledge gap with C++ so I (as a developer) can understand how to design and when to implement this type of macro. Even giving me a link to a guide or something is fine. I tried searching using terms macro, C++, UCLass, Unreal but those terms aren't really coming up with a C++ definition of this macro.

#include "GameFramework/Actor.h"
#include "Pickup.generated.h"

UCLASS()
class BATTERYCOLLECTOR_API APickup : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    APickup();

    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
like image 900
Phil Avatar asked Dec 21 '15 21:12

Phil


1 Answers

The Unreal Engine C++ codebase uses a custom preprocessor, called Unreal Header Tool (UHT) to generate custom Runtime Type Information (RTTI) from your C++ code. It manages to do so by looking for these special macro-like annotations in the code. So pedantically speaking, they are not simple C++ macros, there's more to them than that.

I'm not a user of Unreal Engine, so I'm not aware of the implementation details. I don't know if the UHT preprocessor strips them out after running, or maybe they are just defined to nothing for the C++ compiler, e.g.:

#if !RUNNING_UHT
    #define UCLASS()
#endif

Both approaches seem valid.

Is this normal C++?

It depends. It's certainly not Standard C++. Macros are less used in C++ because the language provides better options in most cases (e.g.: inline functions, enums, const, templates), so you probably won't see that kind of macro usage in most codebases.

like image 177
glampert Avatar answered Nov 15 '22 22:11

glampert