6 karma · joined March 11, 2019
// 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 a lot of decent ways to use templates; that link is definitely not one of them.
Generally I find the best use of templates is the most basic: you want to write some code that operates on duck-types. As long as one is able to control themselves and pick the right use cases for maintainability, templates are simple enough. In my experience, this is very doable.
Concepts are also a really cool way to rein in which types are accepted as a template parameter and have a lot of potential to make templates easier to understand and teach. To me, this is the most exciting c++20 feature.
The author did indicate that they had problems with oom and memory eviction so it probably is a memory limited use case.