Calypso
Calypso copied to clipboard
foreach over C++ iterator
import std.stdio : writefln;
pragma(cppmap, "<vector>");
import(C++) *;
void main()
{
ℂcpp.std.vector!int list;
list.push_back(1);
list.push_back(2);
list.push_back(3);
list.push_back(4);
foreach(value; list)
writefln("value: %s", value);
}
rangefor.d(13): Error: no property popFront for type ℂcpp.std.vector.vector!int, perhaps import std.range; is needed?
Importing std.range doesn't help.
There are multiple workarounds, but it would be nice if C++ iterators were supported in foreach. Should Calypso handle this?
cpp.std.range
has a irange
function to foreach over C++ containers:
import cpp.std.range;
foreach(value; list.irange)
writefln("value: %s", value);
But you're right that it'd be nicer if C++ classes were handled differently by foreach, I'll look into it.