Limitations of C++ range proposal

Ali Çehreli acehreli at yahoo.com
Fri Sep 20 11:26:28 UTC 2019


On 09/16/2019 02:47 PM, Ali Çehreli wrote:

 > auto interspersed(R, D)(R r, D delim) {
 >    struct MyRoundRobin {
 >      bool doDelim = false;
 >
 >      auto empty() {
 >        return r.empty;
 >      }
 >
 >      auto front() {
 >        return (doDelim ? delim : r.front);
 >      }
 >
 >      auto popFront() {
 >        if (!doDelim) {
 >          r.popFront();
 >        }
 >        doDelim = !doDelim;
 >      }
 >    }
 >
 >    return MyRoundRobin();
 > }

That function causes an embarrassing closure allocation. Instead,

1) Both 'r' and 'delim' should be copied as members of the struct and
2) The struct should be defined as 'static'

I wrote something longer on the Learn forum:

   https://forum.dlang.org/thread/qm2cni$1ra7$1@digitalmars.com

Ali




More information about the Digitalmars-d mailing list