5 ms·
The amount of background knowledge you need to understand what’s happening here... I don’t envy people who have to catch up becoming more and more convinced th
by rjeli 7y ago
The amount of background knowledge you need to understand what’s happening here... I don’t envy people who have to catch up
becoming more and more convinced that when sfinae opened the Turing hatch it doomed c++
- jcelerier 7y agoI think that it's pretty easy if you just consider that what's happening between `template<` and `>` is a simple LISP whose values are types.
- rjeli 7y agomore like a prolog with obtuse syntax and complicated semantics
- AlexCoventry 7y agoScott Meyers' next Effective C++ is going to be a doozy. Is it even possible to usefully categorize all the edge cases anymore?
- jchb 7y agoScott Meyers has retired from C++ (his own blog post: http://scottmeyers.blogspot.com/2015/12/good-to-go.html?m=1 http://scottmeyers.blogspot.com/2015/12/good-to-go.html?m=1) so the next C++ bible has to come from somewhere else. Maybe he saw it coming :)
- Nursie 7y agoIt just looks kinda wrong to me - If your generic template has to switch on what type it's been passed, surely you've made it too generic somewhere? Adding back conditional compilation or execution afterwards strikes me as the same sort of code-smell as checking if something is an instance of a given class before then casting it and calling a member - you've lost information and using some sort of reflection to get it back looks like poorly thought out code. Sure, there are times when you more or less have to, but you ought to try to avoid them. But what do I know, I haven't done C++ for about 5 years and was never that into it.
- ahartmetz 7y agoI have an example for you. A serialization library that supports container classes. It has a generic way to fill lists (vectors), but not all list containers support resize and default-initialize new elements. So somewhere inside a generic template function there is a switch to use or replicate that function.
- gpderetta 7y agoYou do not switch on types, you switch on groups of tuoes having certain properties. It is not a new thing this sort of thing has been doable for the last 20 years in a form or another.
- gumby 7y agoThink of it the other way around: there’s a generic method/template, but the developer has a special implementation for a specific case. The compiler already does this for things like memmove() that is properly aligned and sized; this allows user code to cleanly do the same thing.
- p0nce 7y agotraits + "constexpr if" replace SFINAE.
- gpderetta 7y agoYou often need sfinae to implement the trait itself, but concepts (and the 'requires' keyword itself) will finally get rid of sfinae.
- saagarjha 7y ago> The amount of background knowledge you need to understand what’s happening here... Not a whole lot, actually. You can go from C to this in about a week if you’re focused. (I did this a few years ago when learning C++. Now if you want to talk about initialization, that’s a whole ‘nother story…)
- tomnj 7y agoYou don’t need sfinae to get Turing completeness of templates. Template specialization (a crude form of pattern matching) is the main driver for Turing completeness. For example, here’s a toy project to implement a subset of scheme lisp in templates which needs no sfinae: https://github.com/tdp2110/TmpLisp https://github.com/tdp2110/TmpLisp
- lenkite 7y agoI seriously don't understand this. I am a developer learning Modern C++ and this article was perfectly clear to me. I was using `enable_if` before, but the new `if constexpr` does make things clear and idiomatic.
- gumby 7y ago> …when sfinae opened the Turing hatch it doomed c++… I think that’s unfair. While “sfinae’ itself is a mouthful, what it says in practice is that template expansion observes the principle of “least surprise” which is inherently user friendly. You certainly don’t need to know the acronym, or even of the idea, to benefit from it. I stead, the developer won’t be presented with a confusing error message when the thing they want to happen is supported but not the first possibility examined.
- skrebbel 7y agoI just learned about sfinae by googling it, so correct me if I'm wrong, but I think you're misreading the GP's comment. The argument is I think, that because sfinae effectively added an "if" statement to C++ templates, it made templates turing complete. It made it possible to write all kinds insane, undebuggable programs purely at compile-time, in the most terrible purely functional language imaginable. People did exactly that, which went fine until their colleagues (or users of open source libraries) did something unexpected and got screenfuls of errors. Wikipedia's sfinae page has a nice story about how it added compile-time introspection and conditionals to the language: https://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error https://en.wikipedia.org/wiki/Substitution_failure_is_not_an...
- gumby 7y agoAny powerful language (lisp, c++) has plenty of sharp knives in its toolbox. My point was that this tool made it easier to write code that behaves the way the end user expects. Primarily library writers (c++ is full of weirdo constructs that I never use in my code but which people writing libraries use to valuable effect — and which make my code better). While people complain that “c++ has this hard to understand feature” the real question is “does that feature make writing user code easier” and the committee (in ‘17 and ‘20) I hunk have taken that approach. Sure you can write spaghetti code, but you can do that in almost any language. And there are popular languages that had explici design goals of limiting expressiveness in the hope of reducing spaghetti code (Pascal, in its time, and go today). I consider it like a balloon: without that power you end up with more boilerplate and repeated code, which to me makes following the code harder. But other people reasonably disagree.
- skrebbel 7y agoI'm not sure, I last coded C++ about 8 years ago and that was all C++03, yet I had no trouble to follow along. I mean, even if you never saw a "constexpr" before, isn't it pretty clear from the example and the name, well, "constexpr" want's going on? Same for the other newish C++ features. I thought it was a nice little primer.