Hacker News new | past | comments | ask | show | jobs | submit login

I use the typedef version to head off problems. Anyhow, how about this:

C++:

    template<class T> T func(T t) { ... }
D:

    T func(T)(T t) { ... }
or

C++:

    void f(long);

    void test() {
        f(1); // calls f(long)
    }

    void f(int);
D:

    void f(long);

    void test() {
        f(1); // calls f(int)
    }

    void f(int);
or

C++:

    int f(int a[3]) { return a[4]; } // undefined behavior
D:

    int f(int[3] a) { return a[4]; } // index out of bounds error
Of course, you can use `array<int>` in C++, and all the above can be worked around, but it just is more work and doesn't look as good.



C++20:

  template <typename T> T func(T t) { return t; }
  auto func(auto t) { return t; }  // same, "abbreviated"
C++ is often not as nice to type as D, or as Rust. That's backward compatibility for you. But having literally tens of billions of lines of code in production, and millions of programmers who know how to use it, counts for a lot.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: