수색…


통사론

  • 템플릿 식별자 (TemplateParameterList) {...}
  • 구조체 식별자 (TemplateParameterList) {...}
  • 클래스 식별자 (TemplateParameterList) {...}
  • 반환 유형 식별자 (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