Use a C/C++ library with function pointer callbacks in Swift with async/await

BTW, it becomes slightly nicer with ".mm" (Objective-C++) :

+ (void)auth:(BasedClientID)clientId withName:(NSString *)token completion:(void (^)(NSString*))completion {
    ccall((void*)CFBridgingRetain(completion), [](void* userData, const char* s) {
        auto completion = (void (^)(NSString*))CFBridgingRelease(userData);
        auto ns = [NSString stringWithUTF8String:s];
        completion(ns);
    });
}

as you can use a lambda function "inline".

I do use a .mm file, nice, however the part [](void* userData, const char* s) gives me a syntax error.. I'm not familiar with it, but: Expected '(' for function-style cast or type construction

If you write just "class C {};" - does it compile ok?
Worth checking .mm file is treated as Obj-C++ by Xcode (by default it is).

image

This is what I have in C++ language dialect settings - again, this is default in Xcode, I didn't change them:

Lambda functions are available since C++11.

So for me it is also a Obj-C++ source, but:

Strange. Check the other settings as per above screenshot.

Does this compile?

// test.mm // Objective-C++ source
#include <stdio.h>
auto x = []{
    printf("hello, world\n");
};

that gives an error: Expected expression

I can get this error when I change "C++ Language Dialect" from the original GNU++20 (that was chosen by default) to C++98 / GNU++98. Peculiarly also when I change it to "Compiler Default" option (which presumably choses that old language dialect). With all other variants starting from C++11 it compiles fine.

ah ok that makes sense indeed.