1

In the C++ Core Guidelines std::optional is only referred once:

If you need the notion of an optional value, use a pointer, std::optional, or a special value used to denote “no value.”

Other than that, it is not mentioned in the guidelines, so in particular there is no recommendation to use it instead of a pointer, when expressing the intent of an optional value.

Are there any disadvantages of the usage of std::optional in comparison to a pointer that might be null in a non-polymorphic context?

So std::optional is only referred as a side note and as the second option to a pointer. To me it seems like that std::optional is much more expressive. When using a pointer, you can never be really sure if the option of a nullptr is intended.

10
  • 1
    this question lacks context. The only part you do quote does mention std::optional. Commented Nov 16, 2022 at 11:31
  • 1
    where do you miss std::optional being mentioned? Commented Nov 16, 2022 at 11:36
  • 7
    "Why is the usage of std::optional not recommended" It is recommending std::optional Commented Nov 16, 2022 at 11:39
  • 2
    std::optional does not dynamically allocate the stored object. Whether this is an advantage or disadvantage depends, but it is not equivalent to passing a pointer. Suppose you have a function like this: void foo(SomeCustomTypeThatIsExpensiveToCopy *);. You wouldnt refactor this to use std::optional (at least not without also doing heavy refactoring on the calling code) Commented Nov 16, 2022 at 11:39
  • 3
    @PaulSanders wandbox.org/permlink/2cfuOMy2ep9EhaWw ;) Commented Nov 16, 2022 at 11:41

1 Answer 1

3

That guideline is not listing things in order of "pick this first". It is listing options that are appropriate in different situations, and leaves it up to you which is most appropriate for your situation.

In particular, choosing between (const)T* and std::optional<T> is the same as choosing between (const)T& and T. Adapting the examples of the enclosing rule, you'd have

// optional<int> is cheap to copy, pass by value
optional<int> multiply(int, optional<int>); 

// suffix is optional and input-only but not as cheap as an int, pass by const*
string& concatenate(string&, const string* suffix); 

Aside: If you follow the other guidelines, then you can be sure that a pointer indicates a situation where "no value" is an expected case.

Sign up to request clarification or add additional context in comments.

2 Comments

Sure, if you can assume that everyone uses the guidelines, then a pointer can always be nullptr and this represents meaning. However, when you read code of someone else, you do not know what principles it follows.
@Henk independent of what guidline one follows, as a writer of the function you have to assume that a pointer can be nullptr, then you are on the safe side. As a user of a function you can assume that it is either documented or nullptr is a valid argument to a function taking a raw pointer, or you can consider the function broken.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.