C++ Ranges proposal for the Standard Library

Olivier Grant via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 17 03:10:21 PDT 2014


On Friday, 17 October 2014 at 09:52:26 UTC, Marco Leise wrote:
> Am Fri, 17 Oct 2014 09:17:51 +0000
> schrieb "ZombineDev" <valid_email at he.re>:
>
>> I saw [this][0] proposal for adding ranges to C++'s standard
>> library. The [paper][1] looks at D style ranges, but concludes:
>> 
>> > Since iterators can implement D ranges, but D ranges cannot 
>> > be used to implement iterators, we conclude that iterators 
>> > form a more powerful and foundational basis.
>> 
>> What do you guys think?
>> 
>> [0]: https://isocpp.org/blog/2014/10/ranges
>> [1]: https://ericniebler.github.io/std/wg21/D4128.html
>
> True. Iterators are more foundational, ranges are more
> neat-o. ;)
> When you look at this C++ function as part of a signal
> processing home work you know why you don't want to see them
> in top level code:
>
> // C++
>
> double mittelwert(const vector<double>& vektor)
> {
> 	vector<double>::const_iterator it;
> 	double summe = 0;
> 	for (it = vektor.begin(); it != vektor.end(); ++it)
> 	{
> 		summe += *it;
> 	}
> 	return summe / vektor.size();
> }
>
> // D (removing iterators)
>
> double mittelwert(in double[] vektor)
> {
> 	double summe = 0;
> 	foreach (wert; vektor)
> 	{
> 		summe += wert;
> 	}
> 	return summe / vektor.length;
> }
>
> // D (using range sum function)
>
> double mittelwert(in double[] vektor)
> {
> 	return sum(vektor) / vektor.length;
> }

No, the equivalent implementation in C++ is this:

double mittelwert(const vector<double>& vektor)
{
    double summe = 0;

    for(auto &x : vektor)
    {
       summe += x;
    }

    return summe / vektor.size();
}


More information about the Digitalmars-d mailing list