16 ms·
Help me sort out the meaning of “{}” as a constructor argument
- gpderetta 10y agoThe answer to the problem can be found in the comments, but not in a single place: X<T>{{}}; will: for T = DefCtor call the initializer_list constructor with a single default constructed element. The outer brakets are for the initalizer list and the inner ones are for the DefCtor constructor. The brackets for T constructor itself are allowed to be elided. for T = NoDefCtor, the previous overload resolution is not valid as NoDefCtor has its default constructor disabled, so the inner brackets match an empty initializer_list, while the outer ones the X constructor. finally, for T = DeletedDefCtor one would expect the same as NoDefCtor, but there is a quirk in the language: explicitly disabling the default constructor still allows aggregate initialization (which is enabled for any class that doesn't have otherwise a constructor); this allows for the same overload resolution as in DefCtor. This is considered a defect and will be hopefully corrected soon. While initializer_list allows for some nice syntactic sugar, I think it is now generally considered to be misdesigned, especially as it breaks the otherwise great uniform initialization syntax ({..}) that was added at the same time in C++11. There is an ongoing effort to fix the it, but it is hard to do without breaking backward compatibility. edit: formatting
- shmageggy 10y agoMy god c++ is atrocious.
- Roboprog 10y agoLike "Wargames", the best way to win is not to play. Between moving from MS to POSIX environments in the 90s, and the "death" of Borland, this left me with few options other than another Faustian deal with Java-land, alas. I'm actually liking Javascript nowadays, though. Especially as more of an FP-capable language than an OOP-mandatory language.
- tdb7893 10y agoI'm loving the fact fact that languages seem to be moving towards some of the better parts of FP and away from some of the worse parts of OOP. I'm not particularly fond of fully FP languages but they have some really nice ideas that I've been enjoying using.
- jholman 10y agoI agree. And for myself at least, I could also say: I'm not particularly fond of fully OOP languages, but they have some nice ideas that I've been enjoying using. If a thing I want to make is a stateless process, modeling it as a function seems nicest. If that function can be truly a function (i.e. pure), that's best. If it can be semi-pure, in the sense that its only access to mutation is via its arguments (e.g. it takes in a DB handle explicitly), that's alright. If it has other access to global state, perhaps reconsider. If a thing I want to make is a container for state, and its state transitions have some structure to them, then I hear there's this thing called an ADT, and OO seems pretty nice. Neither of those statements is an architectural manifesto, and OO design and functional design both seem like bad ideas to me. That is, requiring modules to be instances of language-syntax-level functions or language-syntax-level objects seems like a bad idea to me. I guess if you go read the plain-English descriptions of Smalltalk objects, that matches my idea of how modules should work, except when they should be functions in a plain-English sense.
- lacampbell 10y agoFP and OO are completely orthogonal in my eyes. I can utilise immutable data with referential transparency just as well with Objects as I can with Modules + Structs, but only the latter is called "FP". I think because Java and C++ championed a very impure OO, based around C-like imperative semantics, people regard huge bundles of mutable state as canonical OO. To me that's not the big idea, the big idea is Objects vs Modules + Structs. But I think I'm on the fringe here.
- mikekchar 10y ago
- gpderetta 10y agoplease, if you had 10 years of experience in the language, read every article on C++11/14/17, followed closely the evolution of the language, the answer would have been obvious... ... ok, even I can't say that with a straight face.
- aikah 10y agoC++ is hard. If one sticks to a strict subset it is a nice language. The problem is the code one didn't write. Even the STL is ugly to use. They are trying to fix the language by adding features but it's like Javascript, the old ugly ones are still part of the spec.
- Roboprog 10y agoThe worst part of the JS "enhancements" is trying to make it more like Java, rather than bringing it back to its Lispy + Smalltalky roots. I guess the assumption nowadays by The Management is that programmers are too stupid to learn anything new delimited by other than curly braces, or learn how to backtrace data flow between functions, rather than debugging by property-setter breakpoint.
- nommm-nommm 10y agoCan you give me an example of a "JS enhancement" you are talking about? Its not that I don't believe you I just am curious. I haven't changed the way I, personally, written Javascript since jQuery came out so I'm not up with the new stuff.
- rfrey 10y agoClass syntax - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes https://developer.mozilla.org/en/docs/Web/JavaScript/Referen...
- Roboprog 10y agoYep. TypeScript !== JavaScript, but it's along a similar misdirection. Then there's "let". I guess it's not hurting anything, but how useful an addition is it, really? How many nested, non function, blocks you got in your code, buddy, that you need a new scope for your nested for-loops and else-ifs?!? Between things like forEach, map, filter, etc, I don't actually write very many manual loops anymore, and often use an IIFE for something that outgrows a ternary. Most of my "blocks" are functions.
- pklausler 10y agoBack in the 90's I was working on an operating system kernel that was written in C++. I had read up on the language and had done a lot of compiler work, so I was kind of a local tutor. I'll never forget my boss' reaction when he overheard me tell a coworker "look, if you want to expose your private parts here, you'll need to make a friend."
- nommm-nommm 10y agoThe old idiom I learn in college a long time ago was "only your friends can see your privates."
- weerd 10y agoSo was this comment. Please take the negativity to r/programming.
- jjnoakes 10y agoThere is no need to smear another site.
- mannykannot 10y agoI would not go so far to call it atrocious, but it has demonstrated how a collection of rules, each (for the most part) well thought out, can lead to a combinatorial explosion of complexity, together with some highly unintuitive corner cases. The strong desire for backwards compatibility all the way to C, together with some expedient principle-breaking, has made things more difficult. All of this has been thoroughly investigated and well-documented by Mr Meyers, and I would like to thank him for all his books and articles on the language.
- ot 10y agoA similar defect that was "fixed" breaking backward compatibility was the resolution of initializer_list constructors of recursive types: T(initializer_list<T>); Before, if you did T element; T list = {element}; the second initialization would be dispatched to the initializer_list constructor; after the fix (for GCC, after 4.9) it would call the copy constructor. See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1467 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html... . For us, this changed the semantics of folly::dynamic, which used the initializer_list constructor to construct lists: dynamic element = "hello"; dynamic list = {element}; // ["hello"] before, "hello" after We had to deprecate the initializer_list constructor and fix all the occurrences (https://github.com/facebook/folly/commit/07dc3ce5923b2d761ca9fd7cd53ee890a4ad839d https://github.com/facebook/folly/commit/07dc3ce5923b2d761ca..., https://github.com/facebook/folly/commit/d458b0a14486b9d80d5644c7b4de60640c891df1 https://github.com/facebook/folly/commit/d458b0a14486b9d80d5...).
- gpderetta 10y agoAnd that fix is still far from fixing the language. I had tought that deprecating brace elision for initializer_list and always requiring double braces would be a workable, although backward incompatible fix, but it still cause issues. There was a recent thread on the topic on std-discussion recently. [hi ot, long time no see]
- ot 10y agoYeah, double-braces was also my first guess but found out that Clang and GCC handled it differently (!) so I didn't even try to understand what the std mandates. [hey! :)]
- CJefferson 10y agoThis issue is what stopped me contributing to the C++ standard. We knew brace initialisation was broken before C++11 came out, even std::vector was a mess.
- gpderetta 10y agoWell, the issue is not brace initialization per se, but the ambiguity with initilalizer_list.
- CJefferson 10y agoYes, either feature in isolation would have been an improvement to C++, but their combination was a mess. Probably one of the features shouldn't have been added, or their combination needed some serious tidying.
- johan_larson 10y agoIt seems to me C/C++ has always had problems. K&R C had weird stuff like char pointers used to point to anything. And modern C++ has odd complexities like what Scott's article illustrates. Was there ever a Goldilocks moment when C was just right?
- Roboprog 10y agoC works great, as an alternate to assembler, to bootstrap unix up on meager, 80s style, hardware. Based on things like the shell tools and languages like awk (and other related descendants), I don't think even the unix creators meant for much application level work to be done in C, though, but by assembling components in higher level languages. Try telling that to The Management and all the macho/masochistic Real Programmers, though. Bounds checking? Memory management? (names you can identify to the left of the types, like in Algol, instead of to the right?) That's for sissies!
- pjmlp 10y agoWorse is that even the C creators saw the danger of using C without computer assisted validation and created lint in 1979. https://www.bell-labs.com/usr/dmr/www/chist.html https://www.bell-labs.com/usr/dmr/www/chist.html Yet to this day, many still don't use any sort of static analysis tool.
- Roboprog 10y agoTo be honest, I seldom used lint, either, when I was starting out (although I suppose I cheated by using the Borland IDE in the early 90s). However, when using gcc in the mid to late 90s, it was just too easy to tack on -Wall (enable all warnings) to the options. No real excuse not to always check things, at that point.
- chubot 10y agoI'm struggling with this too... I'm implementing a shell, and on the one hand I've been looking at ancient C code in bash, dash, zsh, mksh, etc. That's clearly not the right way to do it, with raw strings, pointers, and globals everywhere. It's ridiculously verbose and error prone. All recursive parsers in C seem to use setlongjmp for error handling. They have weird memory management with stack allocated strings and globals too, because memory management in C is such a pain. I tend to like "C with classes". When I started working at Google, the C++ style was a breath of fresh air over what I had seen in the game industry (this was in 2005, so C++ was in a very different state). It was very much C with classes: no exceptions, no iostreams, no operator overloading, no default copy constructors or assignment, in/out params, etc. (Lately it has been trending toward "modern C++", albeit with custom internal libraries for errors and such) Then C++ 11 came out -- you really can't do without auto and range for loops. It looks like strongly typed enums are useful, and some STL stuff. For recursive parsers, exceptions instead of setlongjmp seems useful too (setlongjmp doesn't work with destructors AFAIK). I'm scared of R-Value refs and move semantics -- I don't know anything about those. I guess my point is that it's 2016 and it's hard to know if there was a goldilocks moment. Google was using a restricted dialect of C++03 which I think was pretty great and built a lot of real software, but it's hard to say that C++11 didn't add anything useful. I'm sure the Google style diverges pretty wildly from Bjarne's new effort of C++ core guidelines, which I need to look into more: https://google.github.io/styleguide/cppguide.html https://google.github.io/styleguide/cppguide.html https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md https://github.com/isocpp/CppCoreGuidelines/blob/master/CppC... It seems like the LLVM codebase really knows what it's doing in terms of C++, which makes sense, but honestly it's fairly complex for what I want to do. So I think we are just stuck with all these local dialects, which makes code reuse a problem.
- DoofusOfDeath 10y agoI've been programming professionally for over 20 years, most of it in C++. I really like developing fast code, and C++ used to be my favorite tool for the job. But the language complexity gets more insane with every revision. It's now at the point where Scott Freakin' Meyers can't figure out the interpretation of a short little chunk of code. I'm now actively looking for gigs in which I can develop high-performance code in a language _other than_ C++, just because it's such a minefield. I don't follow the discussions of the standards committee carefully, but it seems like they must place almost no premium on keeping the language (and its programs) understandable by non-language-lawyers.
- johan_larson 10y agoAny sense of where the C++ refugees are heading? Rust? Go?
- bnastic 10y ago"C with classes". (I'm not joking)
- Roboprog 10y agoAnd yet, I'm laughing. As you know, that was what we called the C preprocessor thing back in the mid 80s (as discussed in Uni - I never used it until about 1990)
- bitL 10y agoThe most reasonable choice, restricting yourself to a subset. I knew C++ lost it when people started writing library code resembling monads.
- johan_larson 10y ago"When your template system turns out to be Turing-complete, you may have gone too far."
- 10y ago
- seanwilson 10y agoC++ FAQs are full of massively complex conundrums like this for problems you just don't get in other languages. I used to love learning all these highly specific C++ rules and playing language lawyer when I didn't know any better and thought it was a good use of learning time to become an expert. I've since moved on to languages that weren't full of problems like this that let you just get on with what you're meant to be making. If Scott Meyers can't figure this one out what hope would anyone else?
- TeMPOraL 10y agoHaha, same here. I learned C++ as my first real programming language, and I used to like the role of language lawyer at the forums. Still like the role, but now I do appreciate working with simpler (grammar-wise, not feature-wise) languages.
- deleted 10y ago[deleted]
- munificent 10y ago> I've since moved on to languages that weren't full of problems like this I think you mean aren't full of those problems yet. :) There's a lot of nasty corners in C++ but almost all of them are because it effectively has 40 years of wild success. (I say 40 because C++ inherits C's baggage and success.) It's totally reasonable to pick a younger language with fewer warts. Become an expert in it! Be productive! Ship lots of apps. Then, one day, decades and several versions hence, you may find yourself now dealing with all of the warts that language has accrued over time. At this point, you're too invested in it to jump ship. The marginal cost of learning a new corner case is smaller than the cost to start over from scratch and learn 2046's new hot language. Alas, expertise is hard. Everything you learn makes you better at X but slightly disincentivizes you to learn Y, which may not even exist yet.
- seanwilson 10y ago> Then, one day, decades and several versions hence, you may find yourself now dealing with all of the warts that language has accrued over time. What languages are getting even close to being as bad as C++ in this respect though? Java, Python and JavaScript have been around for a while now for example and they seem more opinionated in a way that stops too many warts from appearing. C++ has undefined and compiler dependent behaviour in the core of its design which I think is the real problem. Its complexity just multiplies at an exponential rate as more features and undefined behaviour is introduced.
- chuymax 10y agoThe webpage navigation was less than optimal on a smartphone. Everytime I wanted to scroll left or right the page kept changing. The article was very informative though. Thanks for sharing!
- wfunction 10y agoCan someone explain how this "universal" syntax is supposed to be better than the old syntax?
- gpderetta 10y agoT() had different meaninga depending on T and the context. It could be value initialisation, a cast, a function declaration(!). Also, when used with arguments, you couldn't use it to uniformly initialize aggregates and non aggregates, which is important in generic code. The T{} syntax can be either a constructor call or aggregate initialization and never a cast or function definition.
- couchand 10y agoIt's really hard to read this article on my phone because the code sample overflows to the right and left swipes are aggressively interpreted as a navigation gesture.
- emmelaich 10y agoSomething to consider when judging C++ on the basis a Scott Meyer article is that he tends to find the dark corners. He is an excellent writer and an excellent language lawyer but he has > not written production software in over 20 years, and I have never written production software in C++. Nope, not ever. https://news.ycombinator.com/item?id=9425083 https://news.ycombinator.com/item?id=9425083