Calypso icon indicating copy to clipboard operation
Calypso copied to clipboard

foreach over C++ iterator

Open ThomasBrixLarsen opened this issue 5 years ago • 1 comments

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?

ThomasBrixLarsen avatar Oct 31 '19 15:10 ThomasBrixLarsen

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.

Syniurge avatar Oct 31 '19 17:10 Syniurge