question on [Issue 7853]
Tobias Pankrath
lists at pankrath.net
Mon Nov 5 01:06:46 PST 2012
On 11/05/2012 02:24 AM, Dan wrote:
> Thanks. The reason I'm down this path is something like below. I really
> want to keep const ref for parms on a method (e.g. foo below). It turns
> out the type is a assoc array and length and keys are both giving me a
> headache. Without the cast I get a message like:
>
> Error: function acct.Account.__postblit () is not callable using
> argument types () const
You probably now this, but this error message indicates that you want to
call a non-const method of a const instance.
> Below I cast away const (Dohh!). Is it safe in this case?
Casting away const is okay as long as you don't change a single bit of
your data.
> If not is there another way?
The code below works, too. But to be honest, I don't now why the
compiler complains that the postblit is not callable in your version but
does not actually get called in mine.
-----
alias Account[string] Map;
struct Account {
this(this) const { writeln("acct copied"); }
//this(this) { writeln("acct copied"); }
}
void foo(const ref Map m) {
pragma(msg, "Map is ", typeof(m));
pragma(msg, "Map is ", typeof(cast()m));
writeln("Map has ", m.length);
writeln("Map has ", m.keys);
}
void main()
{
Map map;
Account acct;
foo(map);
Account acct2 = acct;
}
-----
I have added the copy construction of acct2 to verify that the
const-qualified this(this) is actually used for postblit.
More information about the Digitalmars-d-learn
mailing list