4 ms·
Deducing This
- nickthemagicman 6y agoWhat does the domain name mean?
- gpderetta 6y agoit hosts the homepages of a bunch of ISO working groups producing standards for software (ISO C, C++, ADA, POSIX for example). Open because the try to keep the process in the open (as much as ISO allows), sharing publicly at least the working papers and proposals.
- valbaca 6y agostd is the namespace for the C++ standard library
- nickthemagicman 6y agoOh thanks! Makes more sense than what I thought at first!
- ho_schi 6y agoInteresting. But I cannot judge if further syntax is worth the suggested improvements. Is it okay to say that it is better, that the committee decides that for me? I would appreciate actual UTF Support within language more ;)
- icandoit 6y agoAt least the added complexity will result in faster compile times. (jk in a sad way though)
- choeger 6y agoIMO, this article shows what is wrong with C++. 1. The problem statement is well-defined and C++ is the language that cares about these details (compared to, e.g., Java or even Rust). 2. The author correctly identifies a feature in the language that solves the same (or a very similar) problem in a different context with a very hard-to read mechanism invoking special rules. 3. The author proposes additional syntax (essentially necessary for backwards compatibility) that allows to apply the existing mechanism in a new context. 4. The author proposes a new (set of) rule(s) that gives meaning to the new syntax using the existing mechanism. In sum we have: * 1 piece of new syntax * 1 new semantic rule * offloaded a limited problem to a the ever more complex template instantiation machinery * less code in implementations, more in headers Note that the original use case was about how the this argument is passed to methods. But it became a very powerful language extension. (See section 4.2.2) This reduces usability. Imagine a developer calls a method with the wrong const qualifier, yielding an (unexpectedly) const-qualified return type. Currently, there will be a short and helpful error message. With this proposal, this error message will be buried in the template instantiation details. The problem is that template instantiations have become so generic that you essentially program the compiler with them. But how to generate meaningful error messages remains a mystery. Presumably this will be left to the library authors then.
- northwindfoo 6y agoAgreed that modern c++ has bloat. With that said, I believe that the use case that you're describing can be solved by using concepts to constrain the "Self" template parameter, instead of allowing it to be an unconstrained generic type.
- choeger 6y agoI don't think so. Before: "You got a const value /here/ because you called /this/ function which is declared to yield a constant /there/.". Now: "You got a const value /here/ because you called /this/ function with /that/ (huge) template argument we have deduced for you and by a process that is essentially a complex algorithm we think that the return value is const." The function is not going to be constrained because the author wants it to be very flexible.
- northwindfoo 6y agoHmmm I'm trying to imagine the scenario. It sounds like this: // Structure with a function `str`, as in the paper struct StrHolder { // Assume `str` returns a `const std::string&` when `this` is const // and `std::string&` otherwise. template <class Self> decltype(auto) str(this Self&& self) { ... } }; // Let's use it here void main() { const StrHolder s; s.str().size(); // OK; calling a const function on `const std::string&` s.str() = "abc"; // Ill-formed; compilation error here } Did I get the scenario right? If so, from what I could tell from this example, I think the error would essentially amount to "can't call operator= on const-qualified `const std::string&`". After that, it would depend on the compiler in terms of how it prints out template "frames". On Visual Studio, it would probably look pretty ugly. On Clang, might be a bit better. I don't recall how G++ prints it out. After the first line, I think it would look mostly like how any template function is expanded, which is... not that pretty. All in all, I agree it would be a bit uglier than a plain const decorated function, but I don't think that it's bad enough to dismiss the feature, though I admit that this is subjective.
- gpderetta 6y agoThis feature will greatly simplify CRTP while also making it safer and more powerful. It will also remove the need to of multiple pointless copy pasted overloads with different reference qualification on this. On the balance I think the change will be an improvement. Concepts could be used to make the error messages better, but TBH I don't have high expectations on concept error messages (they are still useful for other reasons though). edit: > Currently, there will be a short and helpful error message. With this proposal, this error message will be buried in the template instantiation details. if there is a single overload, there is no point in using this feature. If there are multiple overloads, the error message is already not going to be very nice. edit2: In retrospect I think that the language should have had explicit 'this' from the beginning a-la python, with no difference between member functions and free functions (except for virtuals possibly) and '.' should have been just syntactic sugar. IIRC Bjarne thinks the same.
- baybal2 6y agoChange the title to something less cryptic.
- jasode 6y agoTo submitter (ingve): In the title, "this" is a C++ keyword that shouldn't have been capitalized to "This". I also agree with sibling comment that an alternative title such "Deducing this (A C++ syntax proposal)" would save a click for many readers who aren't interested in C++.