4

I'm writing a small C++11 library in which I believe std::optional would be a nice addition in some functions which can return nullptr. However, std::optional is a C++17 feature. Since being C++11 is a requirement, I'm looking for ways to use std::optional while keeping compatibility.

I found that feature macros can be tested. I suppose I could use it to detect whether std::optional is available... but what's the best approach when it isn't?

Should I provide my own std::optional implementation?

Return nullptr when std::optional isn't available? (Likely to mess my code.)

Or give up on the idea and keep returning nullptr only?

rodrigocfd
  • 6,450
  • 6
  • 34
  • 68
  • 8
    What about `boost::optional`? – Yksisarvinen Aug 21 '19 at 19:41
  • @Yksisarvinen Possible, but I'd rather keep the library depending only on the STL itself. – rodrigocfd Aug 21 '19 at 19:42
  • 4
    If C++11 is a requirement, you cannot use `std::optional`. Choose one. – Barry Aug 21 '19 at 19:44
  • 2
    You can always make your own. AFAIK it does not rely on any C++17 features, it just wasn't added until then. – NathanOliver Aug 21 '19 at 19:45
  • 1
    Another thing you can do is use `std::unique_ptr` – NathanOliver Aug 21 '19 at 19:46
  • 4
    Why, `std::optional` is not that complex. Find some reliable implementation and/or write some template code. You won't get that class from anywhere else in C++11. – MasterAler Aug 21 '19 at 19:46
  • 1
    If you have code that works correctly without `std::optional` just use it. Don’t complicate things with maybe-this-way-maybe-that-way. – Pete Becker Aug 21 '19 at 19:50
  • 2
    IMHO the question is if you want to return something (an object) that is on the heap or if you want to retrun a value (not created on the heap - here optional helps). If you only need to support heap objects you could mimic the optional API with a wrapper that uses `std::unique_ptr`. (_Note: my company used a std::optional backport to C++11, so it is possible to use it with C++11._) – Sonic78 Aug 21 '19 at 21:36
  • @Mas "Not that complex" [1500 lines](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/optional) – Aykhan Hagverdili Jan 10 '23 at 16:07

3 Answers3

3

There is no standard way of using std::optional in C++11. You can either depend on C++17, or you cannot use std::optional.

Should I provide my own std::optional implementation?

You can write your own optional implementation, but you cannot call it std::optional. Alternatively, you can use a pre-existing implementation such as the one in Boost.

All that said, if you're returning a pointer anyway, then there is probably not much point in using optional since pointers already have a representation for "empty" value: the null pointer. If however you need to distinguish null from empty, then optional may be useful.

eerorika
  • 232,697
  • 12
  • 197
  • 326
3

use this header: https://github.com/TartanLlama/optional

It is the equivalent of std::optional. But it works on C++11 also. When you upgrade to C++17, switch your code to #include <optional>.

Grigory Rechistov
  • 2,104
  • 16
  • 25
Grim Fandango
  • 2,296
  • 1
  • 19
  • 27
0

You should not make return type dependent on C++ Standard version. Standard version switches are meant to be able to compile different parts of a program with different values. If you behave differently based on this, you'll break ODR.

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79