News for hackers. Stuff that matters.
Re:Deducing This by northwindfoo on Thursday March 18, 2021 @07:32AM (#26501590)
Agreed 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.
Re:Deducing This by choeger on Thursday March 18, 2021 @08:07AM (#26501796)
I 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.
Re:Deducing This by northwindfoo on Thursday March 18, 2021 @12:43PM (#26504584)
Hmmm 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.