74 points · by signa11 · 2y ago · 51 comments
Don't get me wrong. This article was very helpful because now, when I eventually see auto&& ...x in a function declaration, I'm hopefully going to remember that the feature is called "parameter packs", and will find this article again with a search if I can't remember what it means.
But realistically, I'm not clever enough to write code that makes use of the various implementation specifics discussed here (stuff like "init-capture packs" that let you efficiently capture a parameter pack into a lambda, and dozens of other similar cases). Having a summary article that omits all that would be more readable for me.
I've had this same feeling about most C++ features lately like coroutines and concepts. Authors take great pains to explain how I might write a library that uses coroutines, but as an app developer I'm never going to do that — I'll be using somebody else's higher-level library because that's how it works in companies with these modern C++ codebases. OTOH, that also means it's hard to write generic documentation from the user point of view because it's often so tied to specific libraries.
I get what you're trying to say, but this assumption that only a subset of developers need to know how to write code intended to be consumed by third parties is the reason why stuff needlessly breaks all the time for no good reason. We can't have modularity without having code consumable by other components, and knowing how to write reusable components and reusable interfaces is a core competency in software development.
For a lot of use cases, a plain C API wrapper is still very useful because it’s so easy to call from practically any other language. It can be better for maintenance to have a very narrow C API than to expose all the bells and whistles from C++ wonderland.
It is an ease-of-use improvement as much as a modularity improvement and provides some evolutionary pressure to design better interfaces to modular code.
But if you know about them, maybe next time you have a problem that it is awkward to do without, you'll reach out for them. Or not if there is a simpler solution.
using FormatArg = variant<int, float, string>;
string FormatArgs(const char* fmt, const vector<FormatArg> &args);
template <typename... Args>
string Format(const char* fmt, Args&&... args) {
vector<FormatArg> expanded_args = {args...};
return FormatArgs(fmt, expanded_args);
}
int main() {
cout << Format("Hello {} pi {}", "world", 3.14f);
}
https://godbolt.org/z/v5zo99aGePacks are great, but more work is still needed to make them first class features of the language.
And, I mean folds? How cool is that!
How to use fold expressions on operator comma isn't even touched on.
https://www.scs.stanford.edu/~dm/blog/param-pack.html#comma-...
C++ template / overload resolution involves some very convoluted logic. And it can take some work to even notice if / how it went differently than a programmer expected.
Programmers typically have logging, interactive debugging, etc. to investigate complicated algorithms like this. But (almost?) no C++ compiler provides such tooling for investigating the behavior of this algorithm.
IMHO it's an area where C++ tooling could serious use improvement.
This pattern is getting easier and easier in modern C++ as more and more thing are made constexpr.
My personal theory is that compiler authors have not provided a template equivalent, because they are embarrassed about the code that's being produced.
(If you disagree, prove me wrong!)
https://cppinsights.io/lnk?code=dm9pZCBmb28oYXV0bykge30KCnZv...
Template don't "produce code". They get expanded well after parsing. There is no code to be embarrassed for.
Also you can ask GCC to dump any intermediate state: https://gcc.gnu.org/onlinedocs/gcc/Developer-Options.html.
Aside from the fact that the articles shows many use cases where packs and fold expressions obviate the need for recursion, what's so bad about recursive template instantiations?
<size_t ...Idxs>
construct work, when we needed same_as<char>?The functionality can be computed as a simple constexpr function.
A string as a char template parameter pack is a textbook example of an antipattern.
A place where this kind of pattern is useful is in having a generic accessor type for fields, and wanting to extend it to tuples. So for example accessor(x) might return x.field, and accessor.name might be "field." To make it work for tuples, you need a string for every possible tuple index. E.g.:
template<typename T, size_t N> struct tuple_accessor;
template<typename...T, size_t N>
struct tuple_accessor<std::tuple<T...>, N> {
constexpr decltype(auto) operator(auto &&t) const {
return get<N>(std::forward<decltype(t)>(t));
}
static constexpr const char *name = index_string<N>();
};One aspect I note is that finally the point is not having variadics or not (assuming an expression can represent a type then you can use tuples instead, which is what styx-lang does). The point is that one must have a way to *use* them.
The "consume" idiom, while working, is not great as it lead to template instances explosion. Not great but not so terrible as finally `static foreach` can only work with a kind of monomorphization too.
I'm really not sure what type of problem variadics solve that can't be done simpler with something else, like default arguments, std::optional, containers like vector, etc.
If you do not need variadics that's fine, you do not have to force them where they don't belong.
They did that already. C++03 -> C++11.
Other languages marched forward and left it in the dust with things like lambdas, reflection, etc.
* Are good at documenting
* Communicate what and why they chose it during code reviews
* Encapsulate their work behind sensible functions/APIs that don't require intimate knowledge of complex internals
And if they aren't, that's worth bringing up to them. If it's still a problem it's worth bringing up to your team/project lead or otherwise.