On Tuesday, 7 May 2013 at 06:41:25 UTC, Meta wrote:
template Test(alias N)
if (isIntegral!(typeof(N)))
{
    struct S
    {
        typeof(N) n = N;

        auto opAdd(T)(T rhs)
        {
            //Error: argument S to typeof is not an expression
            pragma(msg, typeof(T));
            //Error: variable rhs cannot be read at compile time
            return Test!(n + rhs.n);
        }
    }
    auto st = S(N);
    alias Test = st;
}

void main()
{
    auto a = Test!2;
    auto b = Test!3;
    writeln(typeof(a).stringof ~ " a = ", a, ", ",
            typeof(b).stringof ~ " b = ", b, ", ",
            typeof(a + b).stringof ~ " a + b = ");
}

I don't really understand why either of these error messages are occurring. The first is just incomprehensible, and the second seems like it should work. In this case, rhs is fully accessible at compile time in the expression (a + b), so why does the compiler complain?

First error occures,because typeof(T) is undefined - T is a
type,not value.
About second error,although in this case rhs is
accessible,compiler can't know
that you won't call this function in runtime,so it's expected -
it's just ordinary function.

Reply via email to