logoalt Hacker News

pwdisswordfishq • today at 6:37 PM • 2 replies • view on HN

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.


Replies

tialaramex • today at 6:55 PM

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.

layer8 • today at 6:39 PM

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

➕ show 1 reply