www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - An issue with templates with non-existant symbols

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
void foo(T)(T t) if(is(X == struct)) { }

void main()
{
    foo(4);
}

This prints out:

test.d(9): Error: template test.foo(T) if (is(X == struct)) does not
match any function template declaration
test.d(9): Error: template test.foo(T) if (is(X == struct)) cannot
deduce template function
from argument types !()(int)

which is your typical template instantiation error. The real issue is
that the symbol "X" doesn't actually exist.

I've had a template I was refactoring and I've accidentally mistyped
the name of the type, e.g.:

void foo(T)(T r) if (is(T == struct) || is(t == class))  // t should have been T

I was hoping for a better error message here. Can the compiler check
that `is` has an expression with valid symbols in it?
Aug 21 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 08/22/2011 04:16 AM, Andrej Mitrovic wrote:
 void foo(T)(T t) if(is(X == struct)) { }

 void main()
 {
      foo(4);
 }

 This prints out:

 test.d(9): Error: template test.foo(T) if (is(X == struct)) does not
 match any function template declaration
 test.d(9): Error: template test.foo(T) if (is(X == struct)) cannot
 deduce template function
 from argument types !()(int)

 which is your typical template instantiation error. The real issue is
 that the symbol "X" doesn't actually exist.

 I've had a template I was refactoring and I've accidentally mistyped
 the name of the type, e.g.:

 void foo(T)(T r) if (is(T == struct) || is(t == class))  // t should have been
T

 I was hoping for a better error message here. Can the compiler check
 that `is` has an expression with valid symbols in it?

No, because that would defeat the main purpose of 'is'. 'is' is to a large extent there to allow you to check for conditions that are not necessarily semantically valid. However, the compiler could catch that the result of some parts of the constraint does not actually depend on any template arguments to improve the error message. Maybe you could write an enhancement request for this.
Aug 22 2011