Remix.run Logo
▲ pwdisswordfishq 4 hours ago

Indeed. And forcing all object creation to be routed through the "constructor" concept leads to a whole host of problems. For example:

    std::vector v("foo", "bar");
This should create a vector of two elements, right? Well, too bad; it crashes instead. Why? Because the inferred type is std::vector<char>, and it attempts to construct a vector from a pair of iterators. Why? Because the only way to distinguish between different modes of constructing an object is through overloading, and overloads are too often ambiguous.

Arthur O'Dwyer's crusade against CTAD (<https://quuxplusone.github.io/blog/tags/#class-template-argu...>) ultimately boils down to this problem, which is not really type deduction itself, but ambiguous overloads, which are hard to avoid when constructors are the only way to create an object, without resorting to clumsy workarounds like tag types (which create problems of their own).

There are other problems too, like the fact that you cannot call std::make_unique (or emplace) for a class with a private constructor even in a scope where you have access to the constructor itself.

▲tialaramex 4 hours ago | parent | next [-]

Yes, the ergonomics are just better

If you Vec::with_capacity(5) both you and a reviewer know what's going on here - this is clearly a Vec with capacity 5.

When you call a C++ constructor with 5 as the parameter, both you and the reviewer had better be sure what that 5 means.

▲edflsafoiewq an hour ago | parent | prev | next [-]

IIRC, one reason C++ uses overloading for all constructors is if the construction occurs in a template, the caller can select the constructors from outside by eg passing the correct arguments. If the constructor were named, there would be no way to pass in the name from outside the template.

▲layer8 4 hours ago | parent | prev [-]

These aren’t problems with the constructors per se, though. E.g. Java doesn’t have any of these problems.

▲pwdisswordfishq 4 hours ago | parent [-]

Java absolutely does have the ambiguous overload problem.

    public class MyVector {
        public MyVector(int... items) {
            System.out.println(String.format("constructing a vector of %d items", items.length));
        }

        public MyVector(int start, int end) {
            System.out.println(String.format("constructing a vector from the range [%d, %d)", start, end));
        }
    }
How do you construct a MyVector of two items?

In JavaScript, how do you use new Array(...) to create an array consisting of a single integer? You can do the same with a single value of any other type.

The reason these languages don't have the make_unique or emplace problems is because they don't have a concept of in-place construction at all, since objects in those languages are heap-allocated and have reference semantics anyway.

▲vips7L 18 minutes ago | parent | next [-]

It’s ambiguous to the reader the JLS strictly defines which constructor would be called here. It’s the int a, int b constructor.

▲layer8 4 hours ago | parent | prev [-]

Right, but that’s a relatively rare problem, and doesn’t usually result in crashes.

More importantly, you have exactly the same problem if you overload a regular method that way. So it’s a problem with overloading, not with constructors.

> How do you construct a MyVector of two items?

    new MyVector(new int[] { 23, 42 })
The correct solution, of course, is to provide named static constructor methods instead (e.g. `MyVector.ofRange(start, end)`).

I do agree that support for named actual constructor methods would be nice. But they would still be constructors.