Xcode does not respect '__builtin_available' availability API

I'm writing a simple Command line application on macOS with code using C++, using a API called 'std::to_chars' from charconv

void foo(void)
{
  if (__builtin_available(macOS 10.15, *))
  {

    char buffer[10];
    std::to_chars_result tcr = std::to_chars( buffer, buffer+5, 5 );
#pragma unused (tcr)

  }else{
    return;
  }
   
}

Since Xcode complains main.cpp:19:41: error build: 'to_chars<int, 0>' is unavailable: introduced in macOS 10.15

I wrapped the code with an availability check API in C++, __builtin_available(macOS 10.15, *)) But even with this availability check API, Xcode still failed to compile.

Anyone knows why?

Answered by snej in 794341022

I ran into this too and figured it out. The issue is that the to_chars functions’ availability attributes use the “strict” modifier, which means it isn’t possible to check for them at runtime. From the Clang docs:

The flag strict disallows using API when deploying back to a platform version prior to when the declaration was introduced. An attempt to use such API before its introduction causes a hard error.

So unfortunately, to use these functions in an app that supports older OSs, you have to use compile-time checks with ifdefs.

I ran into this too and figured it out. The issue is that the to_chars functions’ availability attributes use the “strict” modifier, which means it isn’t possible to check for them at runtime. From the Clang docs:

The flag strict disallows using API when deploying back to a platform version prior to when the declaration was introduced. An attempt to use such API before its introduction causes a hard error.

So unfortunately, to use these functions in an app that supports older OSs, you have to use compile-time checks with ifdefs.

Xcode does not respect '__builtin_available' availability API
 
 
Q