TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

C++11 and Boost - Succinct like Python

201 pointsby daivdover 12 years ago

28 comments

viraptorover 12 years ago
"you will notice that it is more or less as succinct as the Python code." ??? I hope that this person writes better python than this:<p><pre><code> const map&#60;const string, const tuple&#60;int, int, StrToStr&#62;&#62; TagDataMap { {"title" , make_tuple( 3, 30, stripnulls)}, {"artist" , make_tuple( 33, 30, stripnulls)}, ... </code></pre> it will be succinct when the whole type can get reduced to "auto". Python would just do:<p><pre><code> TagDataMap = { 'title': (3, 30, stripnulls), 'artist': (33, 30, stripnulls), ... </code></pre> Ord implementation is also crazy:<p><pre><code> int i = static_cast&#60;unsigned char&#62;(s[0]); return (boost::format("%1%") % i).str(); </code></pre> considering it doesn't even handle different string encodings.<p>I don't want to complain about C++. It's a great language and has its uses. But it's not succinct and it's far away from what scripting languages provide. Claiming otherwise is close to fanboyism. Not being succinct is not a bad thing. There are many other things that C++ does for you that Python doesn't.
评论 #4723526 未加载
评论 #4723033 未加载
评论 #4723610 未加载
Jabblesover 12 years ago
Is it too late for me to bang my Go drum?<p><a href="http://play.golang.org/p/53jSv32wSF" rel="nofollow">http://play.golang.org/p/53jSv32wSF</a><p>(You can't access the filesystem on the playground, so it doesn't run.)<p>* Look at that error handling. Mmmmhmmm, clear and explicit. If something goes wrong, I'll know about it.<p>* Apart from, of course, errors that I ignore, such as when converting the year from 4 chars to an int (line 54). I don't care if I can't parse that.<p>* Note the difference (lines 54, 56) between parsing the track (which is a byte), and the year, written as 4 digits. The byte can just be cast to an int, the string needs to be parsed by the strconv package.<p>* I have a little bit of defensive programming in the form of a panic(), which would tell me if something that I thought was impossible has happened.<p>* defer on line 19 makes sure the file closes if we managed to open it, regardless of when we return.<p>* type casts are explicit, even from int to int64 (line 20)<p>* I don't specify which interfaces a type implements, the compiler handles that all for me.<p>* Parse() returns map[string]interface{}, which allows me to store anything (in this case just ints and strings) in a key-value store.<p>* A type-safe printf! "%v" (line 67) uses reflection to look at the type of the argument and deduce the natural format. So I can pass it an int or a string and it works :)<p>* On line 86 I pass this weird new type to a printf function and it goes ahead and uses the String() method that we defined. If we hadn't defined that we'd get a printout of the type, which in this case would be the 128 bytes we read.
评论 #4726624 未加载
评论 #4724842 未加载
Kurtz79over 12 years ago
It's succint, all right (well, sort of).<p>But Python's major strength is readability, even more than coinciseness, or better, to provide both at the same time.<p>C was born as a terse language, sacrificing readability for coinciseness (the original examples in K&#38;R are incredibly succint, almost elegant, but far from readable). I can't see many improvements in C++ (a language that arguably has worse coinciseness than C, without gaining in readability in the process)in that regards, even with the new version.<p>I work with C/C++ and I could easily understand a Python script even when I wasn't that familiar with the language, the same cannot be really said for that code in C++.<p>BTW, I'm not bashing C++, that has a lot going for it,just not readability and coinciseness.
评论 #4723343 未加载
评论 #4724137 未加载
评论 #4724121 未加载
songgaoover 12 years ago
Well, I liked C++11 a lot. But now I think I'll replace it with Go whenever possible. I don't really mind so much the verbose syntax. What matters is how difficult it is to write efficient code.<p>I recently rewrote one of my C++ projects(which uses C++11 features and Boost.asio) in Go. It took me half the time, less than 2/3 lines of code compared to the C++ version. This is expected. But most surprisingly, despite that I tried my best thinking about how to make it as efficient as possible in every detail when writing C++ version, and that I'm quite new to Go, the Go version still achieves nearly 100% higher throughput than C++ version.<p>Maybe my C++ skill sucks. But I guess I'll just let it suck.
评论 #4723171 未加载
评论 #4723178 未加载
ctrlaltescover 12 years ago
What I find with C++ is that there are many languages inside it. Put 5 C++ programmers in a room and you'll probably end up with 6 different styles.<p>That the language may have some Python-esque tendencies (given very particular language, compiler and library versions, and a depth of knowledge not required in the Python equivalent) doesn't seem that interesting. Especially when you consider that 9/10 C++ programmers you meet don't actually code in that style and don't intend to.
huhtenbergover 12 years ago
<p><pre><code> cout &#60;&#60; join(pm | transformed([](propMap::value_type pv){... </code></pre> I'm sorry, but as "succinct" as it is, this is basically an unreadable bullshit. Reminds me strongly of a macro abuse in C.
评论 #4723114 未加载
评论 #4723603 未加载
bcoatesover 12 years ago
This is neat and C++11 is pretty exciting, but one thing that C++ doesn't need is the further propagation of tuples into non-generic code. Requiring make_tuple instead of allowing shorthand was the right decision.<p>Tuples in python are a reasonable tradeoff between not wanting to declare anything and the hassle of anonymous structure. This doesn't apply C++ where the equivalent is the POD struct:<p><pre><code> //In the olden days we could not initialize a map inline like this //Key -&#62; metadata mapping. Unfortunately strutcts cannot be declared //inside a template declaration. struct TagData { int start; int length; StrToStr mapfun; }; const map&#60;const string, const TagData&#62; TagDataMap { {"title" , { 3, 30, stripnulls}}, {"artist" , { 33, 30, stripnulls}}, {"album" , { 63, 30, stripnulls}}, {"year" , { 93, 4, stripnulls}}, {"comment" , { 97, 29, stripnulls}}, {"genre" , {127, 1, ord}}}; </code></pre> Creating a named struct pays off when it's time to use the Map, no extra locals or tie() needed to write clear code:<p><pre><code> //for loops over collections are finally convenient to use. for(auto td : TagDataMap){ //C++ created a horrible precedent by making the data type of //a map pair&#60;K,V&#62; instead of struct ValueType { K key; V value; }; auto tdd = td.second; ret[td.first] = tdd.mapfun(sbuf.substr(tdd.start, tdd.length)); } </code></pre> <a href="http://liveworkspace.org/code/bcd52515fb7161858e974b7ff3c0aac5" rel="nofollow">http://liveworkspace.org/code/bcd52515fb7161858e974b7ff3c0aa...</a>
评论 #4724753 未加载
danieldkover 12 years ago
This post shows that C++11 is actually quite a comfortable language. Programming in C++98/03 was often a necessity for me (good for making performant cross-platform software), while I disliked the language quite a lot. The introduction of type inference, closures, range-based for loops, and uniform intialization makes many things that used to be tedious much simpler.<p>That said, I guess we have to live with C++03 for many more years. E.g. in one application that I co-developed, we use a library that can currently only be compiled without a substantial amount of effort on Visual Studio 2005 or 2008. The DLL compiled with older versions is not compatible with 2010 or 2012. So, we are basically stuck in 2005 or 2008 land for now.
unwindover 12 years ago
Great post, it's always fun to see the cutting edge of C++ revealed. It's such a strange land. :)<p>To me, this post would have been 10X more interesting if there was some elementary benchmarking included. If it's about the same speed as the corresponding Python code, which I'd bet is easier to write for more programmers, then I don't quite see the point being as clearly proven.<p>If it's 100 times faster (or whatever), then it gives more credibility to the idea of writing code like this in C++ to begin with, and to learning all the new language features that makes it safe while being that much faster than Python.
评论 #4723060 未加载
评论 #4722993 未加载
评论 #4722994 未加载
mitchiover 12 years ago
No thanks. I'll use D if I want a ton of features for free. This is not beautiful code. I could work around it I guess because I've seen much worse coming from C++ but it's really not my ideal of C++. But this is a small example, the source code of the STL offers much more madness. Not to mention the compiler messages you get for using templates...
talloaktreesover 12 years ago
I know c and python, but not c++. While reading the code section, i totally thought this was a wonderful satire of c++
评论 #4723399 未加载
arctangentover 12 years ago
Equivalent code written in D would look much more like the Python code than this.
SiValover 12 years ago
Periodically throwing some new ingredients into old soup to freshen it up can only extend the life of leftovers so far. There comes a time when, even with a handful of fresh veggies, it's no longer good soup. You need to toss out the leftovers, scrub the kettle, and start a fresh batch.<p>You will still need C++ for dealing with all the legacy C++ out there, but if you are starting a new project, you ought to be able to get the small, fast, efficient runtime advantages of a legacy monster like C++ from a much smaller, simpler language with a modern standard library.<p>Maybe it will be Go, but even if not, we need a simple, safe, productive language with modern features pre-installed (unicode strings, safe arrays, lists, maps) and a modern standard library that statically compiles to small, fast, native executables.<p>C++ with its "if you use the most recent 10% and pretend the old 90% doesn't exist, it's a great language" ethos is not what we need for new code.
评论 #4727444 未加载
simmonsover 12 years ago
I've been using both C++11 and Python lately. While the new C++11 features add a lot of value, I still find it frustratingly verbose for some things. For example, finding an element in a collection:<p><pre><code> std::string type = "foo"; auto it = std::find_if( channel_widgets.begin(), channel_widgets.end(), [type](const std::shared_ptr&#60;Widget&#62; &#38;w){ return (w-&#62;getType() == type); } ); if (it == channel_widgets.end()) { std::cerr &#60;&#60; "widget not found" &#60;&#60; std::endl; } else { std::shared_ptr&#60;Widget&#62; target_widget = *it; std::cout &#60;&#60; "widget found." &#60;&#60; std::endl; } </code></pre> versus (for example):<p><pre><code> type = "foo" matches = [x for x in widgets if x.get_type() == type]; if matches: target_widget = matches[0] print "widget found:",target_widget else: print "widget not found" </code></pre> I may be missing out on some C++11 feature for doing this better. (Or even some Python feature for doing this better!)
评论 #4724735 未加载
评论 #4724587 未加载
评论 #4724952 未加载
评论 #4724693 未加载
pifover 12 years ago
The only point that finds me in disagreement is the need for a code standard <i>that decides well on which parts should be used and how</i>. I don't understand: why limiting ourselves? Why choosing such a well-tested instrument and choosing NOT to use some of it?
评论 #4724173 未加载
评论 #4723675 未加载
j_bakerover 12 years ago
I think people who jump on the "C++11 is as good as Python/Ruby/Javascript/other-language-of-the-week" bandwagon are missing the point, and that is that limitations are a good thing. <i>Can</i> C++ be made as concise and easily-readable as one of the above languages? I wouldn't doubt it.<p>C++ can be made to do <i>lots</i> of things, and it doesn't achieve that by being elegant. It achieves that by trying to do everything possible under the sun. Because of that, C++ is and always will be much more complex than the other languages.
评论 #4723904 未加载
activepeanutover 12 years ago
Is it possible to build Boost for iOS with c++11 enabled in llvm-clang?
评论 #4723424 未加载
bitcrackerover 12 years ago
In other words: C++11 boosts Python :-)<p>I think only a hardcore C++ developer would claim that the author's sample is "succinct". Honestly, C++11 is still far behind the easiness of Python (or Scheme), even with Boost.<p>Funny, a decade ago Ada 95 (the "military" language for high-critical applications) looked like a monstrous over-designed beast when compared to C++. Today Ada 2012 looks elegant and even "small" when compared to C++11. How times have changed :-)
malkiaover 12 years ago
And here goes your productivity - the real killer application for your link times.
nimrodyover 12 years ago
I work with C++ daily and it still has many rough edges:<p>* Horrible error messages<p>* Even simple programs take ages to compile due to massive header files.<p>LLVM/Clang help on both fronts but it's still quite difficult.<p>D2 seems much more promising if you can do without the libraries.
评论 #4724227 未加载
jamesaguilarover 12 years ago
And then you do an accidental implicit conversion from the iterator type (const pair&#60;...&#62;) to the function type (pair&#60;...&#62;), return a reference to one of the fields and BOOM, everything explodes.
ifij775over 12 years ago
The changes to C++ are too little, too late. These changes should have been made years ago, and C++ has lost momentum and credibility. Is anyone comparing Python to c++? nope, only the other way around.
评论 #4724395 未加载
评论 #4724157 未加载
评论 #4724055 未加载
muyuuover 12 years ago
I think this comparison is completely misplaced. A language like C++11 simply doesn't even aim at being succinct like Python.
abyxover 12 years ago
Succinct - I don't think that word means what you think it means
评论 #4723277 未加载
cjdrakeover 12 years ago
Nice try, Bjarne. I'll stick with Python :).
drivebyacct2over 12 years ago
I hate to do it (who am I kidding, no I don't) but the battle in this thread for static typing, succinctness and an EASY language that is easy to wrap your head around... if those are things that make you happy to program in a language, please give Go a shot. Here, take 10 minutes: <a href="http://tour.golang.org" rel="nofollow">http://tour.golang.org</a>
lbollaover 12 years ago
My eyes just exploded! ;-)
16sover 12 years ago
I find C++ much easier to read than Python. Here's how to reverse a string in C++:<p><i></i><i>string s = "string";</i><i></i><p><i></i><i>reverse(s.begin(), s.end());</i><i></i><p>And here's how to reverse a string in Python:<p><i></i><i>s = "string"</i><i></i><p><i></i><i>print s[::-1]</i><i></i><p>In my opinion, the C++ version is far easier to read and just makes more sense. Edit - This is just one small example, however, I find it holds true for the entire languages in general. Also, I do a lot of Python and C++ systems programming so I use both frequently and while I prefer C++, I think Python is the best scripting language available today.
评论 #4723851 未加载
评论 #4723898 未加载
评论 #4724089 未加载
评论 #4723943 未加载
评论 #4723856 未加载
评论 #4723852 未加载