Thread overview
function(pointer) as template argument, explicit template instantiation
Dec 31, 2021
kdevel
Dec 31, 2021
Tejas
Dec 31, 2021
kdevel
Dec 31, 2021
data pulverizer
Dec 31, 2021
kdevel
Dec 31, 2021
H. S. Teoh
Dec 31, 2021
kdevel
December 31, 2021
class R {
}

void foo (R r)
{
}

alias fn = void function (R);

void lyr (fn F) (R r)
{
}

immutable fn foo_ptr = &foo; // line 14
pragma (msg, typeof (foo_ptr));

auto ptr = lyr!(foo_ptr);    // line 17

dmd reports:

immutable(void function(R))
dptr.d(14): Error: expression `& foo` is not a valid template value argument

If I comment out line 17 the code compiles. I want to put the explicitly instantiated function template into an immutable AA. How can that be phrased such that dmd compiles it?

December 31, 2021

On Friday, 31 December 2021 at 00:57:26 UTC, kdevel wrote:

>
class R {
}

void foo (R r)
{
}

alias fn = void function (R);

void lyr (fn F) (R r)
{
}

immutable fn foo_ptr = &foo; // line 14
pragma (msg, typeof (foo_ptr));

auto ptr = lyr!(foo_ptr);    // line 17

dmd reports:

immutable(void function(R))
dptr.d(14): Error: expression `& foo` is not a valid template value argument

If I comment out line 17 the code compiles. I want to put the explicitly instantiated function template into an immutable AA. How can that be phrased such that dmd compiles it?

Is it okay to use template parameter instead of template value parameter?

class R {
}

void foo (R r)
{
}

void lyr (fp_type, R) (fp_type fp, R r)
{
}

pragma (msg, typeof (&foo));
R r;
void main(){
    auto foo_ptr = &foo;
    lyr(foo_ptr, r);
}
December 31, 2021

On Friday, 31 December 2021 at 00:57:26 UTC, kdevel wrote:

>
class R {
}

void foo (R r)
{
}

alias fn = void function (R);

void lyr (fn F) (R r)
{
}

immutable fn foo_ptr = &foo; // line 14
pragma (msg, typeof (foo_ptr));

auto ptr = lyr!(foo_ptr);    // line 17

dmd reports:

immutable(void function(R))
dptr.d(14): Error: expression `& foo` is not a valid template value argument

If I comment out line 17 the code compiles. I want to put the explicitly instantiated function template into an immutable AA. How can that be phrased such that dmd compiles it?

Pointers are runtime entities and are not suitable template parameters (compile time).

So assuming that you are trying to either pass a function constant of a specific type signature as a template argument, or a function pointer as an argument with either a template specialisation or constraint:

class R {}
void foo(R r){}
alias fn = void function(R);

//function compile time constant
void lyr(fn fp_type)(R r){}

//As template constraint
void con(T)(T fun, R r)
if(is(T == fn))
{
  fun(r);
}

/*
  //As template specialisation
  void con(T: fn)(T fun, R r)
  {
    fun(r);
  }
*/

//Function constant
enum fn foo_ptr = (R r){};
pragma(msg, typeof(foo_ptr));

//Declared at compile time but only executable at runtime
auto ptr = &lyr!(foo_ptr);

void main()
{
  //auto ptr = &lyr!(foo_ptr);//could declare this here
  ptr(new R());
  fn new_ptr = &foo;
  con(new_ptr, new R());
}

December 31, 2021

On Friday, 31 December 2021 at 03:02:08 UTC, Tejas wrote:
[...]

>

Is it okay to use template parameter instead of template value parameter?

class R {
}

void foo (R r)
{
}

void lyr (fp_type, R) (fp_type fp, R r)
{
}

pragma (msg, typeof (&foo));
R r;
void main(){
    auto foo_ptr = &foo;
    lyr(foo_ptr, r);
}

No, the type should be the same. I want to register lyr!(&foo) like this:

   fn [string] reg = [
      "foo": &foo,
      "lfoo": &lyr!(&foo)
   ];
December 31, 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:

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
};
December 31, 2021
On Fri, Dec 31, 2021 at 11:52:21AM +0000, kdevel via Digitalmars-d-learn wrote: [...]
> 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:
[...]

--------
struct R {}

void lyr(alias Fn)(ref R r)
{
	Fn(r);
}

void foo(ref R r) { }

static immutable void function(ref R)[string] reg;
shared static this() {
	// workaround for lack of CT AA initialization
	reg = [
		"foo": &foo,
		"lfoo": &lyr!foo,
	];
}
--------


T

-- 
Why waste time reinventing the wheel, when you could be reinventing the engine? -- Damian Conway
December 31, 2021
On Friday, 31 December 2021 at 12:36:46 UTC, H. S. Teoh wrote:
> ```
> void lyr(alias Fn)(ref R r)
> {
> 	Fn(r);
> }
> ```

Thanks! That helped me reinvent the engine.