Fehler aufrufen C ++ String -Konstruktor von Swift
Posted: 05 Sep 2025, 02:39
Ich versuche, eine C ++- Style-String in Swift (C ++ Interoperabilitätsmodus) zu verwenden. Ich bekomme eine ungewöhnliche Plattform - abhängiges Verhalten. Die folgende Funktion: < /p>
public func readLine_CC(_ handle: UnsafeMutablePointer?, keep_newlines: Bool = true) -> std.string? {
var line: UnsafeMutablePointer? = nil
var lineLength: size_t = 0
if let handle = handle {
let readCount = getline(&line, &lineLength, handle)
if readCount > 0 {
let cpps = keep_newlines ? std.string(line) : std.string(line, lineLength - 1)
free(line)
return cpps
}
}
return nil
}
< /code>
kompiliert und ausgeführt wie erwartet auf macOS. Wenn ich jedoch versuche, es unter Linux (AArch64; Swift 6.1.2) zu erstellen, beschwert sich Swift über den Konstruktor < /p>
string (const char* s, size_t n);
< /code>
und Ausschläber: < /p>
error: cannot convert value of type 'Int' to expected argument type 'std.allocator'
28 | let readCount = getline(&line, &lineLength, handle)
29 | if readCount > 0 {
30 | let cpps = keep_newlines ? std.string(line) : std.string(line, lineLength - 1)
| `- error: cannot convert value of type 'Int' to expected argument type 'std.allocator'
31 | free(line)
32 | return cpps
< /code>
Es ist nicht schwierig, eine Arbeit zu finden, aber ich versuche nur zu verstehen, was hier passieren könnte. Alle Vorschläge geschätzt.
public func readLine_CC(_ handle: UnsafeMutablePointer?, keep_newlines: Bool = true) -> std.string? {
var line: UnsafeMutablePointer? = nil
var lineLength: size_t = 0
if let handle = handle {
let readCount = getline(&line, &lineLength, handle)
if readCount > 0 {
let cpps = keep_newlines ? std.string(line) : std.string(line, lineLength - 1)
free(line)
return cpps
}
}
return nil
}
< /code>
kompiliert und ausgeführt wie erwartet auf macOS. Wenn ich jedoch versuche, es unter Linux (AArch64; Swift 6.1.2) zu erstellen, beschwert sich Swift über den Konstruktor < /p>
string (const char* s, size_t n);
< /code>
und Ausschläber: < /p>
error: cannot convert value of type 'Int' to expected argument type 'std.allocator'
28 | let readCount = getline(&line, &lineLength, handle)
29 | if readCount > 0 {
30 | let cpps = keep_newlines ? std.string(line) : std.string(line, lineLength - 1)
| `- error: cannot convert value of type 'Int' to expected argument type 'std.allocator'
31 | free(line)
32 | return cpps
< /code>
Es ist nicht schwierig, eine Arbeit zu finden, aber ich versuche nur zu verstehen, was hier passieren könnte. Alle Vorschläge geschätzt.