digitalmars.D.learn - dup method const or not?
- Matthias Walter (14/14) Nov 27 2011 Hi,
- mta`chrono (14/14) Nov 27 2011 that's a real good question. it fails with the following error:
- Matthias Walter (4/23) Nov 27 2011 Well, this hack doesn't even work - as it does a shallow copy only,
- mta`chrono (12/12) Nov 28 2011 Okay, what about this hack?
- Steven Schveighoffer (5/18) Nov 28 2011 dup should be const. There is no compiler limitation as to why this isn...
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
Nov 27 2011
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); } ---
Nov 27 2011
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
Nov 27 2011
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; } ---
Nov 28 2011
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
Nov 28 2011