bhartsb
bhartsb

Reputation: 1366

How to get std::optional support in Xcode?

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

Upvotes: 4

Views: 10877

Answers (2)

Marshall Clow
Marshall Clow

Reputation: 16670

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.

Upvotes: 3

bitmask
bitmask

Reputation: 34628

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.

Upvotes: 0

Related Questions