Getting a range over a const Container

Ali Çehreli acehreli at yahoo.com
Thu Jul 19 18:16:09 PDT 2012


On 07/19/2012 06:09 PM, Ellery Newcomer wrote:
> On 07/19/2012 02:51 AM, Artur Skawina wrote:
>> Range!Node opSlice() { return Range!Node(first); }
>> Range!(const Node) opSlice() const { return Range!(const Node)(first); }
>
>
> anyone mind cluing me in on why this is possible?

It is the same as in C++. Considering the hidden non-const this and 
const this parameters, one of the member functions is a better match for 
each call:

import std.stdio;

struct S
{
     void foo(string name)
     {
         writeln("Called on ", name);
         assert(typeid(this) == typeid(S));
     }

     void foo(string name) const
     {
         writeln("Called on ", name);
         assert(typeid(this) == typeid(const S));
     }
}

void main()
{
     auto a = S();
     const b = S();

     a.foo("a");    // matches non-const foo()
     b.foo("b");    // matches const foo()
}

The output:

Called on a
Called on b

Ali


More information about the Digitalmars-d-learn mailing list