Template Parameters: this and alias
Explore how this template parameters allow member functions to access the exact object type they belong to, including qualifiers. Understand alias template parameters that accept variables or callable entities to create adaptable templates. Discover their use in struct templates and functions to enhance code flexibility and reuse.
We'll cover the following...
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.
thisreference 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:
As you can ...