1

When I try this:

#include <optional>                                                                         using namespace std;                                                                        
int main() {
    return make_optional(2) + make_optional(3);
}                                                                                           

I get this:

error: no match for ‘operator+’ (operand types are ‘std::optional<int>’ and
 ‘std::optional<int>’)
    5 |     return make_optional(2) + make_optional(3);
      |            ~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~
      |                         |                  |
      |                         optional<[...]>    optional<[...]>

It seems natural to add optional types same as size_t types. ~~I know Haskell supports this natively~~ (EDIT: this assumption is not entirely correct). Of course I could write a helper function. My intention of asking is to make sure there is no simpler way to do this. And before you suggest, yes I have googled, RTFM'ed, etc.

8
  • 1
    You should ask yourself if you still want to use an optional if you need to do math on the data type: there will be wrapping/unwrapping overhead. Anyhow: C++ is not Haskell. Completely different languages actually, different philosophy, different purposes.
    – JHBonarius
    Apr 19, 2021 at 6:52
  • Why do you think this is uncommon? I can think of several instances where this is needed. Binary search for example. Anyhow, as you said, different languages. Apr 19, 2021 at 8:33
  • ? A binary search doesn't require arithmetic (of the elements) and arithmetic doesn't require binary search. std:optional is quite new in C++ (2017 standard), and it's not a zero-cost abstraction. Boost::Optional is older (2003?). Before that people seemed to be happy just check the result from a std::find (=binary search) and execute code based on that. No common need to remember the 'optionality' of a value and transport that all around. (Or sometimes a pointer with nullptr would be used, to indicate it doesn't have a value). C++ is used for other purposes.
    – JHBonarius
    Apr 19, 2021 at 9:33
  • I think you misunderstood. Binary search (or any search for that matter) may or may not return a valid index. Implemented recursively and with optional, this would involve adding. Anyway, the rest of your comment is probably valid. I don't know much about the history. Apr 19, 2021 at 10:13
  • @sprajagopal: "Implemented recursively and with optional, this would involve adding." That's one way to implement it, but not the only way. C++'s binary searching uses iterators, not indices. And it was implemented in C++98 without any need for optional. Apr 19, 2021 at 13:49

1 Answer 1

1

As you can see here std::optional simply doesn't offer an operator+ member. After all, std::optional is able to contain anything, including types for which operator+ doesn't make sense. What would optional<that_type>::operator+ do for those types?

Clearly, you can write your own free function (modulo const/&/both or whatever you deem appropriate for parameters/return type):

std::optional<int> operator+(std::optional<int> o1, std::optional<int> o2) {
    if (o1) {
        if (o2) {
            return std::make_optional(o1.value() + o2.value());
        }
    }
    return std::nullopt;
}
7
  • Is there a design choice here? I assumed Maybe in Haskell is equivalent to optional in C++. But is that a valid assumption (apart from the available operators)? Apr 19, 2021 at 6:27
  • 3
    @sprajagopal, in Haskell Just 1 + Just 3 is invalid.
    – Enlico
    Apr 19, 2021 at 6:30
  • 2
    I don't understand the logic here. operator+(std::optional<T>, std::optional<U>) could simply require that a suitable + exists: requires requires (T t, U u) { t+u;}. The return type would be std::optional<std::common_type_t<T,U>>. That seems reasonable enough, so why the question in the first paragraph?
    – MSalters
    Apr 19, 2021 at 13:49
  • 1
    Note BTW that this operator+ is not defined in namespace std, which means it will not be found by Argument Dependent Lookup on std::optional. This is unavoidable; it's not your namespace.
    – MSalters
    Apr 19, 2021 at 13:53
  • @MSalters, isn't require a C++20 keyword? If you refer to checking that the input satisfies a concept (one could do it with a static_assert or via SFINAE, I believe), yes, it could be done, but given the meaning that the name of std::optional implies, imho it's not obvious that it should take care of facts that are specific to the one or the other type that you might put in it.
    – Enlico
    Apr 19, 2021 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.