www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template arg deduction

reply Kevin Bailey <keraba yahoo.com> writes:
I'm trying to use some fairly simple template argument deduction, 
but
maybe I'm not getting the syntax correct. C++ doesn't event blink 
at
something like this, but D is giving me:

temptest.d(18): Error: template temptest.func cannot deduce 
function from argument types !()(bar), candidates are:
temptest.d(10):        func(T)(foo!T.bar f)

I guess D can't crack open a type like that?

```
template foo(T)
{
class bar
{
         T t;
}

}

void func(T)(            // 10
         foo!(T).bar f)
{
}

int main()
{
         foo!int.bar fi;
         func(fi);        // 18
}
```
Jul 07 2021
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 7/7/21 12:14 PM, Kevin Bailey wrote:

 fairly simple template argument deduction
It's not the simplest but it is still complicated. :)
 I guess D can't crack open a type like that?
There are other ways of achieving the same thing the simplest of which is probably the following: void func(B)(B f) { } Apparently, there are additional constraints on B, which can be expressed in D with some flexibility, pragmatism, and optimism. Ali
Jul 07 2021
prev sibling next sibling parent Paul Backus <snarwin gmail.com> writes:
On Wednesday, 7 July 2021 at 19:14:52 UTC, Kevin Bailey wrote:
 I'm trying to use some fairly simple template argument 
 deduction, but
 maybe I'm not getting the syntax correct. C++ doesn't event 
 blink at
 something like this, but D is giving me:

 temptest.d(18): Error: template temptest.func cannot deduce 
 function from argument types !()(bar), candidates are:
 temptest.d(10):        func(T)(foo!T.bar f)

 I guess D can't crack open a type like that?
https://issues.dlang.org/show_bug.cgi?id=22111
Jul 07 2021
prev sibling next sibling parent Tejas <notrealemail gmail.com> writes:
On Wednesday, 7 July 2021 at 19:14:52 UTC, Kevin Bailey wrote:
 I'm trying to use some fairly simple template argument 
 deduction, but
 maybe I'm not getting the syntax correct. C++ doesn't event 
 blink at
 something like this, but D is giving me:

 temptest.d(18): Error: template temptest.func cannot deduce 
 function from argument types !()(bar), candidates are:
 temptest.d(10):        func(T)(foo!T.bar f)

 I guess D can't crack open a type like that?

 ```
 template foo(T)
 {
 class bar
 {
         T t;
 }

 }

 void func(T)(            // 10
         foo!(T).bar f)
 {
 }

 int main()
 {
         foo!int.bar fi;
         func(fi);        // 18
 }
 ```
The following works: ```d import std.stdio; template foo(T) { class bar { T t; } } void func(T) ( // 10 T f) // No need to repeat foo!(int).bar :) { } int main() { foo!(int).bar fi;//brackets added, now syntax is correct func(fi); // 18. int couldn't get deduced here return 0; } ```
Jul 07 2021
prev sibling parent vit <vit vit.vit> writes:
On Wednesday, 7 July 2021 at 19:14:52 UTC, Kevin Bailey wrote:
 I'm trying to use some fairly simple template argument 
 deduction, but
 maybe I'm not getting the syntax correct. C++ doesn't event 
 blink at
 something like this, but D is giving me:

 temptest.d(18): Error: template temptest.func cannot deduce 
 function from argument types !()(bar), candidates are:
 temptest.d(10):        func(T)(foo!T.bar f)

 I guess D can't crack open a type like that?

 ```
 template foo(T)
 {
 class bar
 {
         T t;
 }

 }

 void func(T)(            // 10
         foo!(T).bar f)
 {
 }

 int main()
 {
         foo!int.bar fi;
         func(fi);        // 18
 }
 ```
This work: ```d template foo(T){ class bar{ T t; } } void func(alias F : foo!T, T)(F.bar f){ } void main(){ foo!int.bar fi; func(fi); } ```
Jul 07 2021