function(pointer) as template argument, explicit template instantiation
kdevel
kdevel at vogtner.de
Fri Dec 31 11:52:21 UTC 2021
On Friday, 31 December 2021 at 09:01:10 UTC, data pulverizer
wrote:
> On Friday, 31 December 2021 at 00:57:26 UTC, kdevel wrote:
> Pointers are runtime entities and are not suitable template
> parameters (compile time).
The address of a function does not change at runtime. The
question is: Can functions (pointers) be used as template
arguments?
> So assuming that you are trying to either pass a function
> constant of a specific type signature as a template argument,
That is what I want to do. The function template lyr shall be
(explicitly) instantiated in order to put the resulting function
pointer into an AA. The call signature of lyr!(foo) and foo must
be the same.
In C++ this looks like this:
```C++
struct R {
};
// typedef void (* Fn) (R &); // ptr version
typedef void (& Fn) (R &);
template<Fn f>
static void lyr (R &r)
{
// invoke f
}
static void foo (R &r)
{
}
/* ptr version
static const std::map<std::string, Fn> reg = {
{"foo", &foo},
{"lfoo", &lyr<foo>} // <--- func tmpl instantiation
};
*/
static const std::map<std::string, Fn> reg = {
{"foo", foo},
{"lfoo", lyr<foo>} // <--- func tmpl instantiation
};
```
More information about the Digitalmars-d-learn
mailing list