News for hackers. Stuff that matters.
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.

There are no comments on this comment yet.