Getting a range over a const Container
Ali Çehreli
acehreli at yahoo.com
Thu Jul 19 18:18:54 PDT 2012
On 07/19/2012 06:16 PM, Ali Çehreli wrote:
> 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:
Ha ha! The output is worthless when both functions print the same thing. :)
This is better:
import std.stdio;
struct S
{
void foo(string name)
{
writeln("non-const foo called on ", name);
assert(typeid(this) == typeid(S));
}
void foo(string name) const
{
writeln("const foo 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()
}
Now the output is different:
non-const foo called on a
const foo called on b
Ali
More information about the Digitalmars-d-learn
mailing list