# How to write macros in c++?

**URL:** <https://forums.unrealengine.com/t/how-to-write-macros-in-c/452322>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [July 15, 2019, 6:04pm UTC](https://forums.unrealengine.com/t/how-to-write-macros-in-c/452322 "2019-07-15T18:04:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steferich](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steferich](https://forums.unrealengine.com/u/Steferich)\
**Post date:** [July 15, 2019, 6:04pm UTC](https://forums.unrealengine.com/t/how-to-write-macros-in-c/452322/1 "2019-07-15T18:04:54Z")

</div>

Hello,

I was wondering how I can write macros like in blueprints but in c++?

Thanks in advance 🙂  
~Stefan

---

<div class="post-metadata">

**Author:** ![pneuma](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pneuma/32/983845_2.png) [@pneuma](https://forums.unrealengine.com/u/pneuma)\
**Post date:** [July 15, 2019, 7:52pm UTC](https://forums.unrealengine.com/t/how-to-write-macros-in-c/452322/2 "2019-07-15T19:52:25Z")

</div>

Macros in C++ may sometimes be a bit tricky so be careful. You start it with `#define` directive.

`#define macro-name replacement-text`

Example:

`#define PI 3.14159`

Macros in C++ are used by preprocessor (stage before compilation and linking). In the example above it will simply replace all `PI` occurrences in code with `3.14159` number.

Macros can be more complex:

`#define MIN(a,b) (((a)<(b)) ? a : b)`

Example of usage:

`std::cout << "The minimum is " << MIN(42, 8) << endl;`

will result in:

`The minimum is 8`

Experienced C++ developers will suggest you to not use macros as long as possible. Functions and variables are most times a better choice. For example `const float pi = 3.14159;` is better than `#define` because it handles type and compiler can help you more when you will try to do something wrong with it.

More information:

[https://en.cppreference.com/w/cpp/preprocessor](https://en.cppreference.com/w/cpp/preprocessor)

[https://en.cppreference.com/w/cpp/preprocessor/replace](https://en.cppreference.com/w/cpp/preprocessor/replace)

---

<div class="post-metadata">

**Author:** ![Steferich](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steferich](https://forums.unrealengine.com/u/Steferich)\
**Post date:** [August 17, 2019, 5:45pm UTC](https://forums.unrealengine.com/t/how-to-write-macros-in-c/452322/3 "2019-08-17T17:45:21Z")

</div>

This is not what I meant. I’m trying to define a function in c++ that when used in blueprints can have more than 1 input/output. For example like the branch node in bps that have 2 outputs, “true” and “false”.

---

<div class="post-metadata">

**Author:** ![Amneri](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Amneri](https://forums.unrealengine.com/u/Amneri)\
**Post date:** [August 20, 2019, 3:41pm UTC](https://forums.unrealengine.com/t/how-to-write-macros-in-c/452322/4 "2019-08-20T15:41:15Z")

</div>

You need to create a function with an enum parameter from uint8 passed by reference.

The code is the following :

# .h file

```
UENUM(BlueprintType)
enum class EMyBranchEnum : uint8
{
    FirstBranchName,
    SecondBranchName
};

// Blueprint library class declaration...
UFUNCTION(BlueprintCallable, meta=(ExpandEnumAsExecs = Branch))
void SomeBranchFunction(const int & Param1, EMyBranchEnum& Branch);

```

# .cpp file

```
void UMyBlueprintLib::SomeBranchFunction(const int & Param1, EMyBranchEnum& Branch)
{
    if (Param1 > 12)
    {
        // Tell to call first branch node
        Branch = EMyBranchEnum::FirstBranchName;
    }
    else
    {
        // Tell to call second branch node
        Branch = EMyBranchEnum::SecondBranchName;
    }
}

```

By expanding the enum, you can use a maximum of 255 branches.
