www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Weird template error message

reply NaN <divide by.zero> writes:
Stripped down example...

module test;

import std.traits;

auto offsetBy(T,F)(T path, F x, F y)
{
     alias PathFloat = TemplateArgsOf!T[0];

     struct Offseter(Q)
     {
         Q path;
         PathFloat x;
         PathFloat y;
     }

     return Offseter(path, cast(PathFloat) x, cast(PathFloat) y);
}

void Foo()
{
     struct Path(T) { T a; }
     Path!float p;
     offsetBy(p, 1, 1);
}

---->

<source>(18): Error: struct `test.offsetBy!(Path!float, 
int).offsetBy.Offseter` cannot deduce function from argument 
types `!()(Path!float, float, float)`, candidates are:

<source>(11):        `test.offsetBy!(Path!float, 
int).offsetBy.Offseter(Q)`

<source>(25): Error: template instance 
`test.offsetBy!(Path!float, int)` error instantiating

Compiler returned: 1

(ldc 1.17)

If you look at the template parameters in the error message 
you'll see "offsetBy!(Path!float, int)"

which is clearly wrong... is it a compiler bug, or just a crappy 
error message?
May 15 2020
parent reply Paul Backus <snarwin gmail.com> writes:
On Friday, 15 May 2020 at 21:40:20 UTC, NaN wrote:
 Stripped down example...

 module test;

 import std.traits;

 auto offsetBy(T,F)(T path, F x, F y)
 {
     alias PathFloat = TemplateArgsOf!T[0];

     struct Offseter(Q)
     {
         Q path;
         PathFloat x;
         PathFloat y;
     }

     return Offseter(path, cast(PathFloat) x, cast(PathFloat) y);
This line is the problem. Template argument deduction does not work for constructors; you have to write `Offsetter!T(...)`.
May 15 2020
parent NaN <divide by.zero> writes:
On Friday, 15 May 2020 at 21:55:16 UTC, Paul Backus wrote:
 On Friday, 15 May 2020 at 21:40:20 UTC, NaN wrote:
 Stripped down example...

 module test;

 import std.traits;

 auto offsetBy(T,F)(T path, F x, F y)
 {
     alias PathFloat = TemplateArgsOf!T[0];

     struct Offseter(Q)
     {
         Q path;
         PathFloat x;
         PathFloat y;
     }

     return Offseter(path, cast(PathFloat) x, cast(PathFloat) 
 y);
This line is the problem. Template argument deduction does not work for constructors; you have to write `Offsetter!T(...)`.
Ah OK thanks. Not the most helpful error message from the compiler lol.
May 15 2020