www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - shouldn't const cast always be allowed (even if shunned)

reply "Dan" <dbdavidson yahoo.com> writes:
There are times when casting away const is needed.  Structs that
define opCast can get in the way of this.  For instance, the cast
below fails, but I think it should always be allowed. So, if the
source type and cast type are the same except for const 
qualifiers,
there is no need to consider custom opCast members. Is a 
reasonable
suggestion for the language?

The cast here fails because it is considering custom opCast 
methods,
but that is not helpful.

Thanks
Dan

---------------------
import std.bitmanip;
void main() {
   const(BitArray) cba;
   cast()cba;
}
Nov 15 2012
next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2012-23-15 15:11, Dan <dbdavidson yahoo.com> wrote:

 There are times when casting away const is needed.  Structs that
 define opCast can get in the way of this.  For instance, the cast
 below fails, but I think it should always be allowed. So, if the
 source type and cast type are the same except for const qualifiers,
 there is no need to consider custom opCast members. Is a reasonable
 suggestion for the language?

 The cast here fails because it is considering custom opCast methods,
 but that is not helpful.

 Thanks
 Dan

 ---------------------
 import std.bitmanip;
 void main() {
    const(BitArray) cba;
    cast()cba;
 }

That does indeed seem reasonable. File an enhancement request. -- Simen
Nov 15 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, November 15, 2012 15:23:40 Dan wrote:
 There are times when casting away const is needed. Structs that
 define opCast can get in the way of this. For instance, the cast
 below fails, but I think it should always be allowed. So, if the
 source type and cast type are the same except for const
 qualifiers,
 there is no need to consider custom opCast members. Is a
 reasonable
 suggestion for the language?
 
 The cast here fails because it is considering custom opCast
 methods,
 but that is not helpful.
 
 Thanks
 Dan
 
 ---------------------
 import std.bitmanip;
 void main() {
 const(BitArray) cba;
 cast()cba;
 }

http://d.puremagic.com/issues/show_bug.cgi?id=5747 - Jonathan M Davis
Nov 15 2012