Default Constructed Return Value: return {} -- Adi Shavit

Simplify your code using a default constructed return value with `return {}`:

Default Constructed Return Value: return {}

by Adi Shavit

From the article:

It is common for C/C++ functions to return default values, for example, if some internal condition fails.
This is straightforward for native return types. This pattern is often seen in factory methods or functions that return a polymorphic type via a base handle.

If the function has multiple early return points, this default construction needs to be repeated at each such point and each time stating the explicit return type just for calling the default ctor. This is annoying because the compiler already knows the return type.

Add a Comment

Comments are closed.

Comments (1)

0 0

MasseyChris said on Feb 15, 2015 12:11 PM:

Can you please post the code .
Will be really helpful.

Thanks