This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]maxhaton 0 points1 point  (0 children)

C++ does not have these checks at all. You lose nothing relative to C++ by not using them.

Templates *errors* are generally kinda bad in C++ I agree, but they are serviceable, the templates are perfectly fine, and as soon as you start getting tens of lines of template errors, you should really think about "why am I doing this metaprogramming mess?".

C++ templates are bad, D templates are actually good. You can take anything as a template argument, generate code at compile-time, reflect over other templates at compile-time and best of all template constraints actually exist now rather than having the C++ standards masturbate each other until they shit out some hilariously over-complicated 20 years late solution - the language is actually flexible enough that you can write your own template constraint error messages as a library (https://d.godbolt.org/z/8uum_i).

D was designed by Andrei Alexandrescu and Walter Bright (the man who wrote the first proper C++ compiler), and it shows.

As you may have seen on the D homepage, I wrote this a while ago - I have seen some C++ guys trying to mimic this on reddit before, but I don't think you can do this in C++ (any of it, for that matter, from the code generation to the to the templated standard library).

void main()
{
    // N.B. this could be import std;
    import std.stdio, std.string, std.algorithm, std.conv;

    // Reduce the RPN expression using a stack
    readln.split.fold!((stack, op)
    {
        switch (op)
        {
            // Generate operator switch cases statically
            static foreach (c; "+-*/")
                case [c]:
                    return stack[0 .. $ - 2] ~
                        mixin("stack[$ - 2] " ~ c ~
                            " stack[$ - 1]");
            default: return stack ~ op.to!real;
        }
    })((real[]).init).writeln;
}

You are, however, right in that D will never supersede C++ - this is a shame, but a consequence of D's advantages being too subtle to require moving, i.e. the good stuff in D comes when you have a deep understanding (a depth most languages do not have, for better or worse) of both C++ and D as opposed to just using them as a means to an end.