tail const

Michel Fortin michel.fortin at michelf.com
Fri Dec 3 18:19:14 PST 2010


On 2010-12-03 21:02:10 -0500, "Steven Schveighoffer" 
<schveiguy at yahoo.com> said:

> On Fri, 03 Dec 2010 20:40:23 -0500, Michel Fortin  
> <michel.fortin at michelf.com> wrote:
> 
>> I have an idea that would fix those: make a template struct/class  
>> instance implicitly convertible to another instance of that same  
>> template if all members share the same memory layout and each member is 
>>  implicitly convertible to the same member of the other template.
> 
> I had thought of that too, a long time ago, but I wasn't sure if it 
> could  work.  I'd go two steps further:
> 
> 1. all the member variable names must be identical.
> 2. you need to identify that implicit conversion is allowed.
> 
> 1 is for sanity ;)
> 
> struct Pair(T)
> {
>     T x;
>     T y;
> }
> 
> struct ConstPair(T)
> {
>     const(T) y;
>     const(T) x;
> }
> 
> 2 is to ensure you are not able to incorrectly morph data into things 
> it  should not be.  i.e.:
> 
> struct Point
> {
>    int x;
>    int y;
> }
> 
> I don't think you should be able to implicitly cast Pair!int to Point,  
> sometimes you want to define different APIs for the same data.

Just like you, I don't think you should be able to implicitly cast 
Pair!int to Point.

What I was suggesting is that the implicit cast would work only as long 
as the struct/class instance comes from the same template definition. 
That'd actually make Pair!int and Pair!uint convertible between each 
other (because int and uint are implicitly converted from one another) 
but ConstPair, which originate from a different template definition, 
wouldn't be convertible from Pair. Instead of defining ConstPair, you'd 
use Pair!(const(T)) to denote a pair of const elements, and because T 
is convertible to const(T), Pair!T is also convertible to 
Pair!(const(T))... as long as the memory layout is preserved, of course.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list