4 ms·
In my (deliberately) limited C++ experience, I'm basically able to dream up how a program would work in a scripting language, translate all of the datatypes tha
by btilly 14y ago
In my (deliberately) limited C++ experience, I'm basically able to dream up how a program would work in a scripting language, translate all of the datatypes that I'm used to like growable arrays to STL equivalents (std::vector< ... > and std::map< ... > cover most of my use cases), and then after much verbosity and debugging, I get a magically fast program.
However the biggest frustration for me (aside from the verbosity) is that things work until they suddenly don't work, and then it isn't obvious why they don't work from the debugging stack you get back. They often don't work for good reason - but unless you're a C++ expert you don't know that reason.
For example suppose that I have a class that has reference members. Those are fields specified with & - they are kind of like a pointer except that you can't change them, and you use . instead of ->. Because you can't change them, operator= is not generated for you. I didn't notice this until I tried to use one in an std::map< ... > and everything blew up.
However luckily confusing backtrace message plus google usually leads to a useful stack overflow answer, else I'd find this process much more painful.
My one remaining frustration is that I hate how the STL did iterators. In the STL, if you have a data_type< foo > template, you iterate over it with a data_type< foo >::iterator. So you get a std::vector< foo >::iterator, a std::set< foo >::iterator, a std::dequeue< foo >::iterator, etc.
This is great for the compiler, but as a programmer it is backwards. I don't care what kind of data structure this came from, I care what kind of data type I get out. I'd like to see those three examples produce an iterator< foo >. And then you can dream up things like the ability to take an iterator< foo > and a method on foo that will return a bar, to get an iterator< bar >. I am used to having map/grep/etc, and it would be nice to be able to translate list-oriented thinking into C++ with less verbose boilerplate.
- jevinskie 14y agoThe iterator typing is much improved in C++11. It's just auto adt.begin().
- btilly 14y agoThere is less verbose repeating of verbosity. But the types did not change in anything like the way that I would like. Try to translate http://perl.plover.com/Stream/stream.html http://perl.plover.com/Stream/stream.html into C++ iterators to see how the current abstraction limits the ways you can think about a program. By contrast suppose that my hypothetical iterator< foo > type was required to have a pointer overload, and supported the methods done() and next(). Now go back and try to translate that article - you'll find that it is pretty easy.
- X-Istence 14y agoInstead of me trying to understand Perl, tell me what problem you are trying to solve that having an hypothetical iterator<foo> would help solve.
- btilly 14y agoThe interface that I describe can be backed by a collection, data streamed externally (eg from a file), or by something generated on the fly. Therefore you can write code for all three cases that looks very similar. That article demonstrated the power of this idea by writing code that fairly efficiently generates the Hamming sequence, which consists of all positive integers whose only divisors are 2, 3, and 5. (So 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, ...)
- X-Istence 14y agoYou can create your own type of iterator that when dereferenced spits out the next number/item, So long as your "end" iterator never matches your "current" iterator it will continue going on. Your "done" function is when "current" and "end" match. "next()" is "++current".
- reddit_clone 14y agoIndeed. First off, it is unrealistic to expect to rewrite perl/python scripts (much higher level scripting languages) in C++ and expect the same level of succinctness. C++ is for larger , high performance applications. The piper needs to be paid one way or another. If you rewrite a perl script in C++ for performance, the relative increase in verbosity is the price you pay.
- btilly 14y agoI accept that a price has to be paid for performance. However I believe that a substantial amount of verbosity could be removed without impacting said performance. In fact C++11 removes some with type deduction. I also get tired of typing some_big_type foo = some_big_type(); - is there a way to say, "I want this thing, and I want it to get the obvious default initialization?" Because what I type now is straight from the redundant department of redundancy department. (Yes, a typedef can make that shorter. You still write the idea twice.)
- compusa 14y ago> For example suppose that I have a class that has reference members. Those are fields specified with & - they are kind of like a pointer except that you can't change them, and you use . instead of ->. Because you can't change them, operator= is not generated for you. I didn't notice this until I tried to use one in an std::map< ... > and everything blew up. This is what std::reference_wrapper is for. > I don't care what kind of data structure this came from Fortunately, neither does the STL. for_each(begin(foo_container), end(foo_container), some_foo_op); Do you know what kind of container foo_container is? It doesn't matter. It can be a set, vector, list, or even a plain array. > And then you can dream up things like the ability to take an iterator< foo > and a method on foo that will return a bar, to get an iterator< bar > You can do this with std::transform. transform(begin(foo_container), end(foo_container), begin(bar_container), op_to_make_foo_into_bar); edit: I see from your other response that what you actually want is streams or generators, not an iterator.
- btilly 14y agoAs I said, my C++ experience is limited. Thank you for demonstrating a better way to do it. Hopefully I'll remember that the next time that I need it. (Which is likely to be several years from now.) Also you're right that I would like streams and generators. But, to me, it is the same abstraction as iteration. See, for instance, how Python does it.
- marblar 14y agoYou should check out the boost iterator library. Depending on the application, you might be looking for either the function input iterator or the counting iterator.
- btilly 14y agoI have avoided learning boost for the simple reason that on the rare occasions when I need C++, it has been apparent to me that I could easily spend longer trying to evaluate whether anything that is available would be a good fit for my problem, than it would take to be done. Put another way, the effort of learning something needs to be amortized over all of the times that you'll likely use that knowledge. I use C++, rarely enough that I won't get much amortization. (And when I do use it, it is painful enough that I don't want to explore it just for personal enjoyment. Leading to my avoiding C++...)
- plorkyeran 14y agoany_range covers the use-case of a generic iterator that doesn't care about the source: http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/reference/ranges/any_range.html http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/ran... In practice, it's not really useful all that often. It has non-zero overhead so it's not good default choice, and you rarely absolutely need it. Most situations where you'd want to return a value then do something with it can be transformed into passing a callback to the function instead, which lets you use a templated operator() to achieve the same result without the runtime overhead.
- minimax 14y ago"However luckily confusing backtrace message plus google usually leads to a useful stack overflow answer, else I'd find this process much more painful." Template related error messages have historically been a huge pain, but modern versions of Clang and g++ are doing a much better job than they used to. If you haven't tried C++ recently (say last year or two), you might try again with a newer version of Clang.
- vinkelhake 14y agoThe thing about a "generic" iterator is that not all iterators have the same capabilities. So while you might not care about the difference between a bidirectional iterator and a random access iterator, there are people who do and algorithms that can be implemented much more efficiently with the right iterator capabilities. There are examples of iterators that use type erasure, but they still incorporate the iterator category in the type.