Function Parameters without Names?
Ali Çehreli
acehreli at yahoo.com
Sun Feb 20 16:21:12 UTC 2022
On 2/19/22 22:09, Salih Dincer wrote:
> The following doesn't work in a function outside the class:
> ```d
> TYPE foo(TYPE) { return 42; }
> ```
> The compiler gives the following error:
> ```undefined identifier TYPE```
I am not sure whether you are asking a question so apologies if I am
stating the obvious. For (TYPE) to be a template parameter list, there
must be the function parameter list as well:
TYPE foo(TYPE)() { return 42; }
<aside>
By the way, the fact that we can return the "int" 42 even when TYPE is
e.g. 'short' is thanks to "value range propagation". The compilr sees
that the manifest constant 42 fits in a short and compiles the code
without complaint.
On the other hand, the following modification does not compile:
TYPE foo(TYPE)() {
int r = 42;
return r; // <-- Compilation ERROR
// Error: cannot implicitly convert expression `r` of type `int` to `short`
}
void main() {
foo!short();
}
</aside>
Getting back to the main topic, you may or may not need the function
parameter list for a member function:
struct S(TYPE) {
// This is not a template but a function
// with the parameter name not omitted.
// (TYPE is already known at this point.)
TYPE foo(TYPE) {
return 42;
}
// This is a member function template
TYPE bar(TYPE2)() {
// ...
}
}
Ali
More information about the Digitalmars-d-learn
mailing list