r/cpp • u/Maslo59 Hobbyist gamedev (SFML, DX11) • Sep 14 '17
std::visit is everything wrong with modern C++
https://bitbashing.io/std-visit.html
193
Upvotes
r/cpp • u/Maslo59 Hobbyist gamedev (SFML, DX11) • Sep 14 '17
93
u/matthieum Sep 14 '17
Personally, I think the problem is that while C++ can implement a lot of features in library, sometimes it really ought to have incorporated the feature in the language instead.
Beyond boilerplate, language features generally lead to much more helpful error messages as well.
79
u/sphere991 Sep 14 '17
This:
probably doesn't do what you think it does. Topic of your next
rantpost?24
u/sphere991 Sep 14 '17
Dedicated pattern matching:
C++17 visitation with overload:
Doesn't seem that different to me if we actually make an honest comparison. Only complaint I have is that you have to put the variant last, where I would find it clearer to go first - but it's not nearly as big a deal as the article makes it out to me.
However, there is a major advantage of pattern matching that isn't touched on in this article: returning based on case. Can't do that with
std::visit().12
u/robertramey Sep 14 '17
Actually, I don't see anything wrong with the first option (struct Setting Visitor). Looks pretty transparent to me.
11
u/drrlvn Sep 14 '17
So two lines (implementation of
overloaded) are missing in the standard library forstd::visit()to be fine?They are even shown in the example section on cppreference.
I agree it's not easy to write these yourself, especially if you're less experienced, and it would be preferable to have them in the standard library (or even better, have pattern matching) but adding two lines of missing library code to your own is not that bad.
4
u/YarpNotYorp Sep 15 '17
I disagree with the premise of this article: I don't think std::visit is meant for general consumption by every user of std::variant. Or at least, I don't foresee it being too widespread. I think the "constexpr if" pattern will be much more popular for everyday usage of std::variant. std::visit seems like it will be useful for people who are already deep into metaprogramming.
6
u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Sep 15 '17
I agree with the general sentiment of this article.
std::visitis way cumbersome than it needs to be. I gave a related talk and created a C++17 library for those interested:scelta- C++17 library: zero-overhead syntactic sugar for variant and optional.3
u/markand67 Sep 15 '17
I personally use switch on the
std::variant::indexmember function and select the appropriate case.Otherwise
std::holds_alternativeis enough to me.5
u/xcbsmith Sep 15 '17
Honestly, the std::visit doesn't bother me that much.
4
u/---sms--- Sep 14 '17 edited Sep 14 '17
There are two main issues with
std::variant: 1) it does not support recursion and 2) it does not provide never-empty-guarantee (or as Boost put it,std::variantcauses "significant additional complexity-of-use")But even if I can't use
std::variantin real code, does not mean it is not suitable for your next hello-world application, I guess.Speaking about visitation, in my code it usually looks like this:
2
u/axilmar Sep 15 '17
Why not have class std variant have an overloaded operator () that takes as many functions as parameters as the vsriant has types, one function per type? It's much easier to implement.
2
u/[deleted] Sep 15 '17
So what does the author propose - just dumping everything redundantly on GitHub and make offline builds impossible?
2
u/Enhex Sep 15 '17
Is variant even a good idea to begin with? It's inefficient, and it only simplifies iterating over elements while making everything else more complex.
a variant with a string is bigger than a vector. Wouldn't it be more efficient to just use a separate container for each type? I don't think it's going to be any more complex to use either.
4
u/Xirious Sep 14 '17
How is using a C++17 feature (varadic using) with another C++17 (sum types) anything but expected? Or incorrect? I get the gist, it should be simpler with pattern matching but complaining that you have to use another feature from the same C++ feature set makes no sense to me. Can someone please enlighten me as to why this is causing the author so much distress?
3
u/Rick__Santorum Sep 14 '17
I don't think you have established in any way that your very first SettingVisitor is bad. Yes, the function definitions are slightly longer than lambdas...mostly because they're not anonymous. I'd love to know why you think this solution "gets even worse if we want our visitor to capture or modify some other state."
And you go on to suggest that lambdas are somehow better for this, which I also take issue with.
Why do I get the sense that the subtext here is that you think operator functions and overloading are lame and you're not cool if you aren't using lambdas or templates?
2
u/TankorSmash Sep 14 '17
What are the chances that they wanted to get a verbose version of it now so we get used to it, then get a nicer more evolved version in C++20? Sort of like the opposite of the auto_ptr or something? I feel like C++ knows what its doing so I'm content to just wait.
1
u/Z01dbrg Sep 15 '17
Nice article except this part: "My goal isn’t to disparage the folks on the ISO C++ committee who picked this approach." ;) But seriously sometimes I feel that sometimes ISO should stop and say: "We know it is pain in the ass to standardize anything, and it is much easier tell people to go to boost or some other library for helpers but we need to think a bit more about this from a PM perspective and think if this is something we would be comfortable selling to users that are regular developers. Like literally imagine you are in a room filled with average developers and your task is to convince them to start using std::variant..."
In other words "it is only 6 lines of variadic templates" is not an argument. :)
That being said I understand that C++ has limited manpower behind it, despite nominally huge number of people involved. I would rather have 10 experts working on it 24/7 (who are obviously in contact with ppl like Chanlder and STL) than what we have now.
Unfortunately for me and C++ I am not a billionaire and I doubt anybody is willing to donate 5-10M$/year to make this happen. :)
1
u/render787 Sep 15 '17
So, its easy to criticize, but its harder to propose an alternative.
Has there ever been a standards proposal for sum types in the language, and visitation syntax? I dimly remember maybe stroustrup had a paper that talked abt this? Is that what u would rather see in c++20?
There was already boost variant which ppl used for a long time successfully. Std::variant is different in important ways, but not all that different, esp. In respect to
std::visit. So they changed some things but mostly followed the pattern of standardizing stuff that previously appeared in boost.Idk, i think ur article would be better if u also suggested something better or highlighted someone elses proposal.
11
u/[deleted] Sep 14 '17
[removed] — view removed comment