4

I'm trying to use std::optional in an Xcode 12.0 Mac OS project. I'm getting the error: No template named 'optional' in namespace 'std'

#include <optional>

std::optional<int> o;

My settings are (I need libc++ for project):

enter image description here

bitmask
  • 32,434
  • 14
  • 99
  • 159
bhartsb
  • 1,316
  • 14
  • 39
  • That's very odd; I don't have Xcode 12 installed, but I do have the apple command-line tools for Catalina (Apple clang version 11.0.3 (clang-1103.0.32.62)) and your code compiles w/o error for me. – Marshall Clow Sep 20 '20 at 20:08
  • `clang++ -std=c++17 -c so.cpp` – Marshall Clow Sep 20 '20 at 20:09
  • the `` header was added in Xcode11. See the [release notes](https://developer.apple.com/documentation/xcode-release-notes/xcode-11-release-notes) – Marshall Clow Sep 20 '20 at 20:11
  • Have you looked at the build log - specifically, at the commands passed to the compiler? I know your screenshot shows you've selected C++17; can you verify that that is being passed to the compiler? Also, have you tried my command-line from above? – Marshall Clow Sep 20 '20 at 20:28
  • @MarshallClow Thanks for your input. I'm building a plugin from a template project and after more careful inspection I noted and removed '-std=gnu++11' under "Other C++ flags". – bhartsb Sep 21 '20 at 00:56

2 Answers2

3

The version of libc++ that Apple ships as part of Xcode 12 (and 11) includes support for many C++17 features, including optional. See the Xcode 11 release notes.

The text in Xcode that says that libc++ "supports C++11" is in contrast to the standard library (libstdc++ v 4.2.1) that Apple used to ship - that did not support C++11. It does not mean that it only supports C++11 to the exclusion of C++14/17/20/etc.

The OP mentioned in a comment that he had a -std=gnu++11 in the "Other flags" that was causing the problem.

Marshall Clow
  • 15,972
  • 2
  • 29
  • 45
0

The std::optional type is a C++17 feature. Although you are compiling with C++17 in your settings, look closer at the standard library version. It states that the version you are using it only supports up to C++11.

Upgrade your library to use std::optional.

Alternatively you can try to #include <experimental/optional> and see if your library has std::experimental::optional available.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • That's not true; the libc++ that Apple ships supports many things beyond C++11 – Marshall Clow Sep 20 '20 at 20:06
  • @MarshallClow Look at the screenshot. It literally says "LLVM C++ standard library with C++11 support". I have little doubt that there is a libc++ version that supports C++17. But the one OP is using appears not to. – bitmask Sep 20 '20 at 20:20
  • the libc++ project was begun (back in 2010) to provide a standard library implementation to support C++11 - as opposed to libstdc++ 4.2.1 (which is what Apple was shipping at the time, which did not support C++11). I believe that's where that text came from. In any case, I assure you that Apple does not ship (today, or any time since about 2015) a C++11-only standard library implementation. – Marshall Clow Sep 20 '20 at 20:25
  • @MarshallClow Okay, I'd like to believe your assurance. Given the information presented by OP, it just looks very much as if an outdated library was to blame. And so far I don't have a better theory --- and also I don't own an Apple on which to test this. – bitmask Sep 20 '20 at 20:50
  • @bitmask is not in the Xcode 12 release. Also the standard library version, although the name says "c++ 11 support" it purportedly has been updated to support C++17. https://developer.apple.com/forums/thread/114246. Xcode 12 is the latest Xcode. – bhartsb Sep 21 '20 at 00:00
  • @bitmask I'm building a plugin from a template project and after more careful inspection I noted and removed '-std=gnu++11' under "Other C++ flags". bit mask if you update your answer to clarify that "LLVM C++ standard library with C++11 support" also means versions up to C++17 and to check "Other C++ flags" isn't overriding I'll accept it as the answer. And experimental is no longer used. Thanks. – bhartsb Sep 21 '20 at 01:00
  • @bhartsb No, Marshall Clow called it. I'd recommend accepting [their answer](https://stackoverflow.com/a/63985111/430766). – bitmask Sep 21 '20 at 10:49