खोज…


वाक्य - विन्यास

  • टेम्पलेट पहचानकर्ता (TemplateParameterList) {...}
  • संरचना पहचानकर्ता (TemplateParameterList) {...}
  • वर्ग पहचानकर्ता (TemplateParameterList) {...}
  • ReturnType पहचानकर्ता (TemplateParameterList) (ParameterList) {...}
  • पहचानकर्ता! (TemplateInvocationList)

एक टेम्पलेट के साथ फ़ंक्शन

import std.stdio;

T min(T)(in T arg1, in T arg2) {
    return arg1 < arg2 ? arg1 : arg2;
}

void main() {
    //Automatic type inference
    writeln(min(1, 2));
    
    //Explicit type
    writeln(min!(ubyte)(1, 2));
    
    //With single type, the parenthesis might be ommited
    writeln(min!ubyte(1, 2));
}

टेम्पलेट

एक टेम्पलेट के साथ पेश किया जा सकता template । इसमें फ़ंक्शंस और कक्षाएं और अन्य निर्माण शामिल हो सकते हैं।

template StaticArray(Type, size_t Length) {
     class StaticArray {
          Type content[Length];
          
          size_t myLength() {
            return getLength(this);
          }
     }
     
     private size_t getLength(StaticArray arr) {
          return Length;
     }
}

void main() {
    StaticArray!(int, 5) arr5 = new StaticArray!(int, 5);
    import std.stdio;
    writeln(arr5.myLength());
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow