Live data from Hacker News

C++11 and Boost - Succinct like Python

fendrich.se

91–100 of 172 posts

Re: C++11 and Boost - Succinct like Python

#91
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.

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:

  //In the olden days we could not initialize a map inline like this
  //Key -> metadata mapping. Unfortunately strutcts cannot be declared
  //inside a template declaration.
  struct TagData { int start; int length; StrToStr mapfun; };
  const map 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}}};
Creating a named struct pays off when it's time to use the Map, no extra locals or tie() needed to write clear 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 instead of struct ValueType { K key; V value; };
    auto tdd = td.second; 
    ret[td.first] = tdd.mapfun(sbuf.substr(tdd.start, tdd.length));
  }
http://liveworkspace.org/code/bcd52515fb7161858e974b7ff3c0aa...

Re: C++11 and Boost - Succinct like Python

#92
post #8

Earlier quoted context omitted.

I wrote this post. The program is I/O-bound, so the only speed improvement comes from not having to start up the Python interpreter. If the task was CPU bound, you would get a great performance boost (sometimes 100x over CPython), but that is well known. There can be other reasons than performance for writing in C++. Used well, the strong static type system can catch many bugs. I suspect (but I cannot prove) that it…

It won't be as good as Haskell for memory safety. For example, this program, using only C++11 idioms, crashes: #include #include int main() { std::vector v; v.push_back(std::string("Hello")); v.push_back(std::string("there")); for (auto ii = v.begin(), ie = v.end(); ii != ie; ++ii) { v.clear(); std::cout Preventing this sort of thing requires strong guarantees about aliasing (to ensure that "v" can't alias the vector…

“C++11 idioms” include a preference for immutability, which leads to natural factorings. You don’t have to worry about things like iterator invalidation that way.

    #include 
    #include 
    #include 
    #include 
    using namespace std;

    template
    void print(const T& v) {
      copy(begin(v), end(v), ostream_iterator(cout, "\n"));
    }

    int main(int argc, char** argv) {
      vector v{"Hello", "there"};
      print(v);
    }
Also, Haskell is only so safe—at work we’ve taken to rejecting non-total functions in review, because they cause more hassle than they’re worth. By non-total, I refer more to “error” than non-termination, of course.

Re: C++11 and Boost - Succinct like Python

#93
post #13

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.

Too little, too late? There's no zero sum game here. Perhaps you're conflating Internet-popular (which doesn't mean shit) with useful?

Re: C++11 and Boost - Succinct like Python

#94

Earlier quoted context omitted.

But that's a completely different claim. I agree that C++11 is much nicer than old C++. I really like the improvements. I'd be glad to see that argument in his post instead. That's not what he wrote though. The title is "C++11 and Boost - Succinct Like Python", the contents say "almost as painless as in a modern dynamic language like Python". That's what I can't agree with. There's still lots of supporting syntax tha…

The extra syntax is not there for nothing. It's adding type information. Thus allowing error checking or dispatching by type or optimisations at compile time. It is a cost in terms of syntax and readability but it's not for nothing. So for correct programs the end result might be the same in terms of values. For buggy code and runtime speed that's not necessarily the case.

> The extra syntax is not there for nothing.

You could say the same thing about everything old C++ compelled you to type. Or old COBOL, for that matter.

I'm more interested in what the C++ memory allocation style does to verbosity. Memory-handling styles are often more important than the traditional paradigms (functional, OO, etc.) in determining what's reasonable/pleasant to do in a given language.

https://sites.google.com/site/steveyegge2/allocation-styles

Re: C++11 and Boost - Succinct like Python

#95
post #44
post #34

Earlier quoted context omitted.

I call shenanigans. That's true of simple scripts, I'm sure. But you can't seriously claim to me that you can understand decorator idioms, iterables or list comprehensions without a deep understanding of the language. What you say might have been true for Python c. 1998, it certainly isn't true today. Broadly: reading code is just hard. It's much harder than writing code. There are no non-trivial codebases that can b…

List comprehensions are not deep magic. If you use SQL as your frame of reference, it's actually pretty easy to pick up. They also add a hell of a lot to readability once you know what it does.

Your critical phrase phrase being "... once you know what it does.". I didn't say it was hard, I said it required learning and experience to use. The OP claimed that non-expert python programmers could read it without that experience. The same is true for virtually all of C++ too.

Re: C++11 and Boost - Succinct like Python

#96

Earlier quoted context omitted.

It won't be as good as Haskell for memory safety. For example, this program, using only C++11 idioms, crashes: #include #include int main() { std::vector v; v.push_back(std::string("Hello")); v.push_back(std::string("there")); for (auto ii = v.begin(), ie = v.end(); ii != ie; ++ii) { v.clear(); std::cout Preventing this sort of thing requires strong guarantees about aliasing (to ensure that "v" can't alias the vector…

“C++11 idioms” include a preference for immutability, which leads to natural factorings. You don’t have to worry about things like iterator invalidation that way. #include #include #include #include using namespace std; template void print(const T& v) { copy(begin(v), end(v), ostream_iterator (cout, "\n")); } int main(int argc, char** argv) { vector v{"Hello", "there"}; print(v); } Also, Haskell is only so safe—at wo…

"const" doesn't help you in the presence of aliasing:

    #include 
    #include 

    template
    void print(const T& v, U& w) {
        for (auto ii = v.begin(), ie = v.end(); ii != ie; ++ii) {
            w.clear();
            std::cout  v;
        v.push_back(std::string("Hello"));
        v.push_back(std::string("there"));
        print(v, v);
        return 0;
    }
In general there are lots of ways to get non-const access to const data. You'd need a sophisticated form of whole-program alias analysis to eliminate the unsafety here. (Even if you didn't have the second "U& w" parameter, there are other potential ways that "print" could get non-const access to "v"; global variables, TLS, by accessing a member of "v", etc.)

Re: C++11 and Boost - Succinct like Python

#97
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:

    std::string type = "foo";
    auto it = std::find_if(
        channel_widgets.begin(),
        channel_widgets.end(),
        [type](const std::shared_ptr &w){
            return (w->getType() == type);
        }
    );
    if (it == channel_widgets.end()) {
        std::cerr  target_widget = *it;
        std::cout 
versus (for example):

    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"
I may be missing out on some C++11 feature for doing this better. (Or even some Python feature for doing this better!)

Re: C++11 and Boost - Succinct like Python

#98

Earlier quoted context omitted.

But that's a completely different claim. I agree that C++11 is much nicer than old C++. I really like the improvements. I'd be glad to see that argument in his post instead. That's not what he wrote though. The title is "C++11 and Boost - Succinct Like Python", the contents say "almost as painless as in a modern dynamic language like Python". That's what I can't agree with. There's still lots of supporting syntax tha…

The extra syntax is not there for nothing. It's adding type information. Thus allowing error checking or dispatching by type or optimisations at compile time. It is a cost in terms of syntax and readability but it's not for nothing. So for correct programs the end result might be the same in terms of values. For buggy code and runtime speed that's not necessarily the case.

This is what caught my eye too as being really ugly and hard to read.

  const map> TagDataMap {
      {"title" , make_tuple( 3, 30, stripnulls)},
Why can't the compiler figure out the types involved in this map structure itself? The user-defined functions are declared above, make_tuple will be declared in some library, and the others are string/int literals.

Re: C++11 and Boost - Succinct like Python

#99
post #97

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: std::string type = "foo"; auto it = std::find_if( channel_widgets.begin(), channel_widgets.end(), [type](const std::shared_ptr &w){ return (w->getType() == type); } ); if (it == channel_widgets.end()) { std::cerr target_w…

Can't comment on C++11 but the Python version is better written as:

    type = "foo"
    try:
        target_widget = (x for x in widgets if x.get_type() == type).next()
        print "widget found:",target_widget
    except StopIteration:
        print "widget not found"

Re: C++11 and Boost - Succinct like Python

#100
post #63
post #58

I find C++ much easier to read than Python. Here's how to reverse a string in C++: string s = "string"; reverse(s.begin(), s.end()); And here's how to reverse a string in Python: s = "string" print s[::-1] 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 a…

How many other languages do you know? You sound like you know C++ really well and sort of know Python, and aren't clearly distinguishing between abstract readability (as ill-defined as that is) vs. how well you personally know the code you are reading. What's probably wrong with your Python reading is that you have to look up what the third parameter in the slice more than the slice parameter actually being confusing…

In most programming languages the [] operator allows one to access elements in a container.

In this case, we are accessing the position "two colons minus one". I refuse to believe this makes any sort of sense for someone that has no deep knowledge of Python.

Post reply on HN