www.digitalmars.com         C & C++   DMDScript  

D - template instanciation

reply "Philippe Mori" <philippe_mori hotmail.com> writes:
Can we do the equivalent of C++ std::make_pair or std::max in D ?

That is a function that will return an object ?

Does we need to instanciate explicitly a function template?
I hope not!

I would like an example of how make_pair and max could be
implemented in D so that we can uses it as in C++ (without
explicit instance)
Aug 19 2003
parent reply Friedrich Dominicus <frido q-software-solutions.com> writes:
Philippe Mori wrote:
 Can we do the equivalent of C++ std::make_pair or std::max in D ?
 
 That is a function that will return an object ?
 
 Does we need to instanciate explicitly a function template?
 I hope not!
 
 I would like an example of how make_pair and max could be
 implemented in D so that we can uses it as in C++ (without
 explicit instance)
 
 
 
 

template PairTemplate (T,S){ class Pair { public T first; public S second; this (T first_val, S second_val){ first = first_val; second = second_val; } public void set_first (T val){ first = val; } public void set_second(S val){ second = val; } } } int main(){ alias instance PairTemplate(int,int).Pair IntPair; IntPair a_pair = new IntPair(1, 2); printf("pair.first = %d, pair.second = %d\n", a_pair.first, a_pair.second); return 0; } Is that what you want? Regards Friedrich
Aug 19 2003
next sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
Having to write the aliases is a pain, but it's something I could usually
live with.

Sean

"Friedrich Dominicus" <frido q-software-solutions.com> wrote in message
news:bhv56p$13ba$1 digitaldaemon.com...
 Philippe Mori wrote:
 Can we do the equivalent of C++ std::make_pair or std::max in D ?

 That is a function that will return an object ?

 Does we need to instanciate explicitly a function template?
 I hope not!

 I would like an example of how make_pair and max could be
 implemented in D so that we can uses it as in C++ (without
 explicit instance)

template PairTemplate (T,S){ class Pair { public T first; public S second; this (T first_val, S second_val){ first = first_val; second = second_val; } public void set_first (T val){ first = val; } public void set_second(S val){ second = val; } } } int main(){ alias instance PairTemplate(int,int).Pair IntPair; IntPair a_pair = new IntPair(1, 2); printf("pair.first = %d, pair.second = %d\n", a_pair.first, a_pair.second); return 0; } Is that what you want? Regards Friedrich

Aug 20 2003
prev sibling parent reply "Philippe Mori" <philippe_mori hotmail.com> writes:
 I do not know if I was missing something, but how about:
 template PairTemplate (T,S){
      class Pair {

          public T first;
          public S second;


          this (T first_val, S second_val){
            first = first_val;
            second = second_val;
          }


          public void set_first (T val){
            first = val;
          }

          public void set_second(S val){
            second = val;
          }
      }
 }

 int main(){
    alias instance PairTemplate(int,int).Pair IntPair;
    IntPair a_pair = new IntPair(1, 2);
    printf("pair.first = %d, pair.second = %d\n",
           a_pair.first, a_pair.second);
    return 0;
 }

 Is that what you want?

 Regards
 Friedrich

No, not realy. I do not want explicit instance. Can I define a function make_pair such that the following would compile for any type: int main() { prinf("first = %d", make_pair(25, "hello").first); } That is, make_pair would returns an usable object of the appropriate type (in this case PairTemplate(int, string)) ? Can we do something like: template (T, S) /* instance */ PairTemplate(T, S) make_pair(T t, S s) { return /* instance */ PairTemplate(T, S)(t, s); } I'm not sure if instance would be required, allowed or prohibited in the above sample...
Aug 20 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
AFAIK, at this point implicit instantiation is not supported. I don't know
if Walter is persuaded of the need yet, but for my part I think it is
absolutely necessary to be able to do generics. (However, I may be wrong. :)


"Philippe Mori" <philippe_mori hotmail.com> wrote in message
news:bi02tb$2e8l$1 digitaldaemon.com...
 I do not know if I was missing something, but how about:
 template PairTemplate (T,S){
      class Pair {

          public T first;
          public S second;


          this (T first_val, S second_val){
            first = first_val;
            second = second_val;
          }


          public void set_first (T val){
            first = val;
          }

          public void set_second(S val){
            second = val;
          }
      }
 }

 int main(){
    alias instance PairTemplate(int,int).Pair IntPair;
    IntPair a_pair = new IntPair(1, 2);
    printf("pair.first = %d, pair.second = %d\n",
           a_pair.first, a_pair.second);
    return 0;
 }

 Is that what you want?

 Regards
 Friedrich

No, not realy. I do not want explicit instance. Can I define a function make_pair such that the following would compile for any type: int main() { prinf("first = %d", make_pair(25, "hello").first); } That is, make_pair would returns an usable object of the appropriate type (in this case PairTemplate(int, string)) ? Can we do something like: template (T, S) /* instance */ PairTemplate(T, S) make_pair(T t, S s) { return /* instance */ PairTemplate(T, S)(t, s); } I'm not sure if instance would be required, allowed or prohibited in the above sample...

Aug 20 2003
parent reply "Philippe Mori" <philippe_mori hotmail.com> writes:
"Matthew Wilson" <matthew stlsoft.org> a écrit dans le message de
news:bi0rgp$ftf$1 digitaldaemon.com...
 AFAIK, at this point implicit instantiation is not supported. I don't know
 if Walter is persuaded of the need yet, but for my part I think it is
 absolutely necessary to be able to do generics. (However, I may be wrong.


I also think so... otherwise we always have to specify types which is error prone and redundant (if the compiler knows, why tell it again ?). Implicit is needed for function like min, max and others to be easy to uses...
Aug 20 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Philippe Mori" <philippe_mori hotmail.com> wrote in message
news:bi0s5q$gqm$1 digitaldaemon.com...
 "Matthew Wilson" <matthew stlsoft.org> a écrit dans le message de
 news:bi0rgp$ftf$1 digitaldaemon.com...
 AFAIK, at this point implicit instantiation is not supported. I don't


 if Walter is persuaded of the need yet, but for my part I think it is
 absolutely necessary to be able to do generics. (However, I may be


 :)

 I also think so... otherwise we always have to specify types which is

 prone and redundant (if the compiler knows, why tell it again ?).

 Implicit is needed for function like min, max and others to be easy to
 uses...

instance max_min( int ).max max_int; int my_func( int c, int d ) { int b = max_int( c, d ); .... // O.K. its a little verbose at the moment. } not much different from int my_func( int c, int d ) { int b = max<int>( c, d ); .... } allowing int my_func( int c, int d ) { int b = max( c, d ); .... } where `max` is there searched for (as C++ does) in imported templates etc would a require import module.template; (not such a bad idea) but what should the search order be ? instance class global templated ? and what about implicit convertions, ambiguity resolution and code bloat. D templates do need a reduction in their verbosity and some improvement of features but lets not make D a write only lang. reducing the ambiguity (imho) makes things easier especially when it comes to working out that other ppls code is doing.
Aug 20 2003
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
It's when you have to start writing things like "instance max_min( typeof(
T ) ).max(t1, t2)" that it really starts to get annoying.  And really it's
just adding redundancy, and moving type conversion errors to the
instantiation instead of at point of use.  I don't think it will save you
any errors or make your life easier if you have to manually spell everything
out.  Just makes your code bigger.  ;)

Sean

"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bi1e39$1apo$1 digitaldaemon.com...
 "Philippe Mori" <philippe_mori hotmail.com> wrote in message
 news:bi0s5q$gqm$1 digitaldaemon.com...
 "Matthew Wilson" <matthew stlsoft.org> a écrit dans le message de
 news:bi0rgp$ftf$1 digitaldaemon.com...
 AFAIK, at this point implicit instantiation is not supported. I don't


 if Walter is persuaded of the need yet, but for my part I think it is
 absolutely necessary to be able to do generics. (However, I may be


 :)

 I also think so... otherwise we always have to specify types which is

 prone and redundant (if the compiler knows, why tell it again ?).

 Implicit is needed for function like min, max and others to be easy to
 uses...

instance max_min( int ).max max_int; int my_func( int c, int d ) { int b = max_int( c, d ); .... // O.K. its a little verbose at the moment. } not much different from int my_func( int c, int d ) { int b = max<int>( c, d ); .... } allowing int my_func( int c, int d ) { int b = max( c, d ); .... } where `max` is there searched for (as C++ does) in imported templates etc would a require import module.template; (not such a bad idea) but what should the search order be ? instance class global templated ? and what about implicit convertions, ambiguity resolution and code bloat. D templates do need a reduction in their verbosity and some improvement of features but lets not make D a write only lang. reducing the ambiguity (imho) makes things easier especially when it comes to working out that other ppls code is doing.

Aug 21 2003