std::variant<>::get() does not compile with Apple LLVM 10.0

All std::variant functionality that might throw std::bad_variant_access is marked as available starting with macOS 10.14 (and corresponding iOS, tvOS and watchOS) in the standard header files. This is because the virtual std::bad_variant_access::what() method is not inline and thus defined in the libc++.dylib (provided by the OS).

If you want to use std::variant in apps running on older OSes, just use std::get_if. In your example:

if (auto* p = std::get_if<int>(&w)) {
  // use *p
} else {
  // error handling
}

You can also check in advance with w.index() and std:: holds_alternative <int>(w).

EDIT: Also see my answer to the similar problem with std::visit (unfortunately with a less handy workaround)


As it turned out the project was set to macOS 10.14, but not the actual build target, which was still on 10.13. Once I reverted that to inherit the deployment target, the test code started to compile fine.

It's an interesting twist, given that XCode 10 (and hence LLVM 10.0) can be installed and used to build C++17 apps on 10.13.