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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With