Re: function(pointer) as template argument, explicit template instantiation

2021-12-31 Thread kdevel via Digitalmars-d-learn

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.



Re: function(pointer) as template argument, explicit template instantiation

2021-12-31 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Dec 31, 2021 at 11:52:21AM +, 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": ,
"lfoo": !foo,
];
}



T

-- 
Why waste time reinventing the wheel, when you could be reinventing the engine? 
-- Damian Conway


Re: function(pointer) as template argument, explicit template instantiation

2021-12-31 Thread kdevel via Digitalmars-d-learn
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
static void lyr (R )
{
   // invoke f
}

static void foo (R )
{
}

/* ptr version
static const std::map reg = {
   {"foo", },
   {"lfoo", } // <--- func tmpl instantiation
};
*/

static const std::map reg = {
   {"foo", foo},
   {"lfoo", lyr} // <--- func tmpl instantiation
};
```




Re: function(pointer) as template argument, explicit template instantiation

2021-12-31 Thread kdevel via Digitalmars-d-learn

On Friday, 31 December 2021 at 03:02:08 UTC, Tejas wrote:
[...]
Is it okay to use template parameter instead of **template 
value** parameter?

```d
class R {
}

void foo (R r)
{
}

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

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


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


```
   fn [string] reg = [
  "foo": ,
  "lfoo": !()
   ];
```


Re: function(pointer) as template argument, explicit template instantiation

2021-12-31 Thread data pulverizer via Digitalmars-d-learn

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

```dptr.d
class R {
}

void foo (R r)
{
}

alias fn = void function (R);

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

immutable fn foo_ptr =  // 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 = !(foo_ptr);

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

```


Re: function(pointer) as template argument, explicit template instantiation

2021-12-30 Thread Tejas via Digitalmars-d-learn

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

```dptr.d
class R {
}

void foo (R r)
{
}

alias fn = void function (R);

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

immutable fn foo_ptr =  // 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?

```d
class R {
}

void foo (R r)
{
}

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

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


function(pointer) as template argument, explicit template instantiation

2021-12-30 Thread kdevel via Digitalmars-d-learn

```dptr.d
class R {
}

void foo (R r)
{
}

alias fn = void function (R);

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

immutable fn foo_ptr =  // 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?