3 ms·
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` r
by northwindfoo 6y ago
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.