Template Parameters: this and alias

Learn about “this” and alias type of template parameters.

this template parameters for member functions

Member functions can be templates as well. Their template parameters have the same meaning as other templates.

However, unlike other templates, member function template parameters can also be this parameters. In this case, the identifier that comes after this keyword represents the exact type of the object that this refers to.

this reference means the object itself, it is commonly written in constructors as this.member = value.

struct MyStruct(T) {
    void foo(this OwnType)() const {
        writeln("Type of this object: ", OwnType.stringof); 
    }
}

The OwnType template parameter is the actual type of the object that the member function is called on:

Get hands-on with 1400+ tech skills courses.