4 ms·
This is sometimes evident in the standard library. I think they chose just about the most developer-unfriendly interface for `std::visit` of variants. Yes that
by afranchuk 5y ago
This is sometimes evident in the standard library. I think they chose just about the most developer-unfriendly interface for `std::visit` of variants. Yes that interface covers some edge cases that I've _never_ seen in practice (visiting two variants at once??), and results in users writing ugly, verbose code. Then they proceeed to have an `overloaded` example on cppreference that's a useful template to at least make that more palatable for the common case, but naturally that's not in the standard library.
It annoyed me so much I implemented what I consider the sane/common interface: `cvisit(variant, [](Type1) {...}, [](Type2) {...}, etc)`.
Yes, I realize my usage might not match with everyone's, but from my own experience and code I've read, this covers almost every usage (visiting sum types).
- jcelerier 5y ago> Yes that interface covers some edge cases that I've _never_ seen in practice (visiting two variants at once??), heh, this was literally one of my first uses of C++ variants: that job needed a generic set of mathematical functions in a way that emulated a weaker type system (with automated conversions left and right), while preserving the original types as far as possible, and did meaningful conversions otherwise e.g. more-or-less using value = variant<int, float, string, vector<float>>; value clamp(value x, value a, value b);
- afranchuk 5y agoAh yeah numeric sort of variants totally make sense for that! In all of my use cases they have been types that don't have operations defined among each other. But really I'd just appreciate the standard library providing at least a convenience function for the common case over their more powerful but obtuse std::visit.
- gpderetta 5y agoSee overload in the example[1] at cpp reference. overload is an extremely useful helper that it should be in any c++ programmer toolbox. [1] https://en.cppreference.com/w/cpp/utility/variant/visit https://en.cppreference.com/w/cpp/utility/variant/visit
- afranchuk 5y agoThanks for posting the link, I forgot to include it!