www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Comparing template alias parameters

reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Given an index structure like this:

struct Index( alias arr ) if ( is( typeof( arr ) t : U[], U ) ) {
     private size_t idx;
      property pure nothrow size_t get( ) const {
         return idx;
     }
     alias get this;

     invariant( ) {
         assert( idx < arr.length );
     }

     this( size_t value ) {
         idx = value;
     }

     Index opAssign( size_t value ) {
         idx = value;
         return this;
     }
}

would it be possible to do something akin to this:

void foo( T )( T[] arr, Index!arr a ) {
}

void bar( ) {
     int[] a = [1,2,3];
     Index!a idx;
     foo( a, idx ); // ensures idx is a valid index for a
}


Somewhat trivial an application, as this could easily be tested
with an in clause, but imagine that ( arr is Index!(arr).arr )
has to be true.


-- 
Simen
Dec 10 2010
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Simen kjaeraas <simen.kjaras gmail.com> wrote:

 Given an index structure like this:

 struct Index( alias arr ) if ( is( typeof( arr ) t : U[], U ) ) {
      private size_t idx;
       property pure nothrow size_t get( ) const {
          return idx;
      }
      alias get this;

      invariant( ) {
          assert( idx < arr.length );
      }

      this( size_t value ) {
          idx = value;
      }

      Index opAssign( size_t value ) {
          idx = value;
          return this;
      }
 }

 would it be possible to do something akin to this:

 void foo( T )( T[] arr, Index!arr a ) {
 }

 void bar( ) {
      int[] a = [1,2,3];
      Index!a idx;
      foo( a, idx ); // ensures idx is a valid index for a
 }


 Somewhat trivial an application, as this could easily be tested
 with an in clause, but imagine that ( arr is Index!(arr).arr )
 has to be true.
Nobody? Meh. -- Simen
Dec 13 2010