C++ interop: For-in loop requires std.__1.vector<ELEMENT> to conform to Sequence

Using Xcode 15 beta 3 on an M1 running Ventura 13.5 I'm trying C++ interop, but fail to iterate through a std::vector, I get compilation error:

For-in loop requires 

'
std.__1.vector<
	basic_string<
		Int8,
		char_traits<Int8>, 
		allocator<Int8>
	>, 
	allocator<
		basic_string<
			Int8, 
			char_traits<Int8>, 
			allocator<Int8>
		>
	>
>
' 

to conform to 'Sequence'

The C++ property in question is std::vector<std::string> dwellingNames;

I thought this document section "Using" stated that we could do for in in Swift on std::vector...? What am I missing?

Perhaps the culprit is that it is a vector of std::string, is iterating over that not supported?

SWIFT_OBJC_INTEROP_MODE has been set to objcxx...

Such type should automatically conform to CxxRandomAccessCollection, so that is definitely a bug. Could you file an issue on GitHub - apple/swift: The Swift Programming Language and attach a reproducer (Xcode project is fine).
CC @egor.zhdan

2 Likes

Thank you for your prompt reply, I was able to create a minimum project demonstrating the issue, here it is: GitHub - Sajjon/CxxRandomAccessCollectionNotWorking: Demo of `CxxRandomAccessCollection` not properly generated

As you can see in code and README, std::vector<int> is callable from Swift, but std::vector<std::string> is not.

1 Like

Thanks, I filed an issue on GitHub for this: std::vector<std::string> fails to conform to CxxRandomAccessCollection · Issue #67410 · apple/swift · GitHub

1 Like

Just merged a patch that fixes this ([cxx-interop] Import iterator types that are not typedef-ed by egorzhdan · Pull Request #67482 · apple/swift · GitHub), thanks for discovering this issue @Sajjon!

3 Likes