The unary plus operator in C++ is one of those lesser known and even less frequently used operators in the language. I have been programming in C++ for many years and only recently started to use it when I found some nifty use cases. The first such case I found in the Carbon language compiler source code, where it was used to convert lambdas into function pointers. The second use case is in creating a convenient function for casting an enum class into its underlying type, which I will describe in this article.

Casting Enum Class to Underlying Type

In modern C++ we have the enum class construct (or enum struct) which should be used as it fixes some issues with C style enums1. One feature of this is that we have to explicitly cast to get an integer value, and when you’re needing the underlying value in many places then it becomes a burden.

The correct way to do this is to do a static cast to the underlying type of the enumeration. In C++ this is properly done by using the expression static_cast<std::underlying_type_t<EnumType>>, but as you can see it is pretty verbose. If it only needs to be used in a handful of places then using this construct will be fine, but if it needs to be used multiple times in the same piece of code then it will be a distraction from the actual intent of the code.

Some might want to use C-style casts, and while it does reduce the total amount of characters in the code, it brings with it many issues and removes the type safety that using an enum class provides. So in this case you might as well just use a plain enum instead, and if you’re fine with this you can stop reading here.

Some others might just explicitly put the type into the static_cast<> in order to remove the rather verbose std::underlying_type_t<> part of the expression. While this is a much better choice than a C-style cast, it will still require a lot of code changes if the underlying type of the enum ever changes, and there is a potential for silent problems if someone forgets to update a cast to the new type.

Leveraging Unary Operator+

Now this is where we can overload the unary operator+ for our specific enun class in order to create a convenient notation to convert the enum to its underlying type. This ends up being a pretty simple bit of code, just a function which wraps up the static cast to the underlying type, like so:

enum class Foo : uint16_t;

constexpr auto operator+(Foo value)
{
	return static_cast<std::underlying_type_t<Foo>>(value);
}

Which can be used in code like this:

enum class Foo : uint8_t
{
	A, B, C, // ...
	COUNT,
};

constexpr auto operator+(Foo value)
{
	return static_cast<std::underlying_type_t<Foo>>(value);
}

constexpr Value ValueFromFoo(Foo foo)
{
	static constexpr Value values[+Foo::COUNT] = { /* ... */ };
	return values[+foo];
}

Additionally in Visual Studio you can add the [[msvc::intrinsic]] attribute to the operator function it to make it more performant in debug builds2.

However this is not the best example of where a function like this is needed, as there are only two places where the unary operator+ is used. I will present a much more extensive example of where this can be used to great benefit in a future article.

Caution

Lastly a word of caution, only use this where it is absolutely necessary, preferably only defining it in C++ source files near where it is needed, and only for enum classes that actually need the functionality. My reasoning for this is that this is a single character function converts a strongly typed enumeration to an integral type which can easily and accidentally be used in arithmetic operations. Same as you wouldn’t automatically provide an operator++ and operator-- for an enum class unless you actually need to iterate over it in some code.


  1. Issues include leaking enumeration names into the enclosing namespace and implicit conversion to integer types. 

  2. Improving the State of Debug Performance in C++