16

I tried to compile the following program with different compilers (including gcc 6.1) :

#include <optional>
int main()
{
    std::optional<int> o1;
}

Output is

main.cpp:1:20: fatal error: optional: No such file or directory #include optional

This is even true for the examples given here: http://en.cppreference.com/w/cpp/utility/optional/optional

Any clues why?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
scoulomb
  • 630
  • 2
  • 7
  • 19

2 Answers2

24

std::optional will be part of the C++17 standard, but if you want to use before then you will have to use std::experimental::optional instead, available in the header <experimental/optional>.

SirGuy
  • 10,660
  • 2
  • 36
  • 66
9

It is in experimental (TS):

#include <experimental/optional>

example:

http://coliru.stacked-crooked.com/a/09ab8d1e51680a79

#include <experimental/optional>
#include <iostream>
int main()
{
    std::experimental::optional<int> o1;
}
marcinj
  • 48,511
  • 9
  • 79
  • 100