Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm playing with the C++17 std::variant type and tried to compile the cppreference example code for get():

#include <variant>
#include <string>

int main()
{
    std::variant<int, float> v{12}, w;
    int i = std::get<int>(v);
    w = std::get<int>(v);
    w = std::get<0>(v); // same effect as the previous line

//  std::get<double>(v); // error: no double in [int, float]
//  std::get<3>(v);      // error: valid index values are 0 and 1

    try {
      std::get<float>(w); // w contains int, not float: will throw
    }
    catch (std::bad_variant_access&) {}
}

in XCode 10. My project is set to C++17, but I get compiler errors:

Call to unavailable function 'get': introduced in macOS 10.14

and

'bad_variant_access' is unavailable: introduced in macOS 10.14

which is surprising in 2 ways: it should compile if std::variant is supported and the hint about macOS 10.14 is weird, given that I'm on that version and it has nothing to do with the supported C++ dialect (and the project's deployment target is 10.14).

Is this something I'm doing wrong or a bug in clang?

like image 454
Mike Lischke Avatar asked Sep 26 '18 15:09

Mike Lischke


2 Answers

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)

like image 198
Tobi Avatar answered Nov 12 '22 15:11

Tobi


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.

like image 5
Mike Lischke Avatar answered Nov 12 '22 13:11

Mike Lischke