Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 27, 2011 dup method const or not? | ||||
---|---|---|---|---|
| ||||
Hi, Recently, I realized that several "dup" methods in D2's phobos are declared like the one for BitArray: @property BitArray dup() My question is why it is declared without "const"? Is it a bug or is there a reason for it? I can think of a case where one only has implemented a shallow copy and wants to omit the const due to the involved transitivity, but this shouldn't be the case for BitArray (the underlying array is dup'ed). Is there a general rule/recommendation when to make a dup method const and when not? My problem arises in a copy constructor for a struct S which takes a const(S) instance. But as S has a BitArray member, duping is not allowed, so I need a way to duplicate a const(BitArray) object. Matthias |
November 27, 2011 Re: dup method const or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthias Walter | that's a real good question. it fails with the following error: Error: function std.bitmanip.BitArray.dup () is not callable using argument types () const here is a hack: --- import std.bitmanip; import core.stdc.string; void main() { const(BitArray) foo; BitArray bar; memcpy(&bar, &foo, BitArray.sizeof); } --- |
November 27, 2011 Re: dup method const or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mta`chrono | On 2011-11-27 23:48, mta`chrono wrote:
> that's a real good question. it fails with the following error:
>
> Error: function std.bitmanip.BitArray.dup () is not callable using
> argument types () const
>
>
> here is a hack:
>
> ---
> import std.bitmanip;
> import core.stdc.string;
>
> void main()
> {
> const(BitArray) foo;
> BitArray bar;
>
> memcpy(&bar, &foo, BitArray.sizeof);
> }
Well, this hack doesn't even work - as it does a shallow copy only, immediately setting bits of bar also changes bits of foo!
Matthias
|
November 28, 2011 Re: dup method const or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthias Walter | Okay, what about this hack? --- import std.bitmanip; import core.stdc.string; void main() { const(BitArray) foo; BitArray bar; bar.len = foo.len; bar.ptr = foo.ptr[0 .. foo.dim].dup.ptr; } --- |
November 28, 2011 Re: dup method const or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthias Walter | On Sun, 27 Nov 2011 16:52:56 -0500, Matthias Walter <xammy@xammy.homelinux.net> wrote:
> Hi,
>
> Recently, I realized that several "dup" methods in D2's phobos are
> declared like the one for BitArray:
>
> @property BitArray dup()
>
> My question is why it is declared without "const"? Is it a bug or is
> there a reason for it? I can think of a case where one only has
> implemented a shallow copy and wants to omit the const due to the
> involved transitivity, but this shouldn't be the case for BitArray (the
> underlying array is dup'ed). Is there a general rule/recommendation when
> to make a dup method const and when not? My problem arises in a copy
> constructor for a struct S which takes a const(S) instance. But as S has
> a BitArray member, duping is not allowed, so I need a way to duplicate a
> const(BitArray) object.
dup should be const. There is no compiler limitation as to why this isn't const. You should file a bug.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation