Decision on container design
Simon Buerger
krox at gmx.net
Tue Feb 1 11:30:55 PST 2011
On 01.02.2011 20:01, Michel Fortin wrote:
> On 2011-02-01 12:07:55 -0500, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> said:
>
>> With this, the question becomes a matter of choosing the right
>> default: do we want values most of the time and occasional
>> references, or vice versa? I think most of the time you need
>> references, as witnessed by the many '&'s out there in code working
>> on STL containers.
>
> What exactly is "most of the time"? In C++, you pass containers by '&'
> for function parameters, using '&' elsewhere is rare.
>
> One thing I proposed some time ago to address this problem (and to
> which no one replied) was this:
>
> ref struct Container { ... } // new "ref struct" concept
>
> void func(Container c) {
> // c is implicitly a "ref Container"
> }
>
> Container a; // by value
> func(a); // implicitly passed by ref
>
> Containers would be stored by value, but always passed by ref in
> functions parameters.
>
Thats would not be "per-value" as most understand it.
Container globalC;
func(c);
void func(Container paramC)
{
c.add(42); // modifies globalC in reference-semantics
// leaves globalC as it was in value-semantics
}
Your Idea would be somehow truly value-based if you default not only
to "ref" but to "const ref", because then the function would not be
able to alter globalC. But making parameters default-const was not
considered the right way for D.
- Krox
More information about the Digitalmars-d
mailing list