www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dup method const or not?

reply Matthias Walter <xammy xammy.homelinux.net> writes:
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
next sibling parent reply mta`chrono <chrono mta-international.net> writes:
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
parent reply Matthias Walter <xammy xammy.homelinux.net> writes:
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
parent mta`chrono <chrono mta-international.net> writes:
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
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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