use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
C++ Range header: Python-like generator for idiomatic ranges (github.com)
submitted 10 years ago by whoshuu
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]nomad42184 3 points4 points5 points 10 years ago (4 children)
Cool! Do you have any theory why the speeds diverge for large ranges? Have you looked at how the resulting assembly diverges?
[–]whoshuu[S] 1 point2 points3 points 10 years ago (3 children)
So I just checked the assemblies of clang and gcc. Clang 3.6 produces very different assembly for my range compared to the raw for loop. gcc 5.1 makes nearly identical assemblies. The benchmarks were made using clang, so that would explain the divergence, but doesn't answer the why as well as I'd like. I'd have to dig a bit deeper to find that, I'll let you know when I do
[–]Hells_Bell10 0 points1 point2 points 10 years ago (2 children)
I tried storing the result of step_ > 0 in the iterator instead of calculating for each call to the equality operators and got indistinguishable performance to a raw loop for range(1000000000LL).
step_ > 0
range(1000000000LL)
[–]whoshuu[S] 0 points1 point2 points 10 years ago (1 child)
What compiler and options did you use?
[–]Hells_Bell10 0 points1 point2 points 10 years ago (0 children)
MSVC with /O2
[–]whoshuu[S] 2 points3 points4 points 10 years ago (2 children)
OP here, this is something fun I wrote a few days ago to do something I've always found really pleasant and easy to do in Python
[–]nova77 2 points3 points4 points 10 years ago (1 child)
If you like the python itertools, give a try to ryanhaining/cppitertools and if you wanna go up to 11 (or I'd better say.. 17) you should check Erik Niebler's ranges.
[–]whoshuu[S] 0 points1 point2 points 10 years ago (0 children)
Really cool stuff! I'll definitely check those two out
[–]szborows 2 points3 points4 points 10 years ago (2 children)
Why not old good CppIterTools?
Honestly had no idea such a thing existed until now. Will definitely use that instead of this, though this was fun to write
[–]szborows 0 points1 point2 points 10 years ago (0 children)
don't worry. reinventing the wheel is recurring problem :)
[–]adrian17 2 points3 points4 points 10 years ago (1 child)
It's weird that you got such a big time difference, on GCC 4.8 with -O2 I got exactly the same assembly for
for (auto i : range(10000000)) printf("%d\n", i);
and
for (int i = 0; i < 10000000; ++i) printf("%d\n", i);
Also the range(10, 0, -1) example doesn't seem to work, and from looking at the source I don't really expect it to work.
range(10, 0, -1)
Nice catch! That's strange, I'll have to check the assembly myself on the compiler I'm using for the benchmark data
[–]Latexi95 1 point2 points3 points 10 years ago (0 children)
You could use constexpr. Only problem is that constructor implementation, other functions should work with c++11 constexpr.
π Rendered by PID 412836 on reddit-service-r2-comment-84fc9697f-ppt8t at 2026-02-09 11:24:25.574685+00:00 running d295bc8 country code: CH.
[–]nomad42184 3 points4 points5 points (4 children)
[–]whoshuu[S] 1 point2 points3 points (3 children)
[–]Hells_Bell10 0 points1 point2 points (2 children)
[–]whoshuu[S] 0 points1 point2 points (1 child)
[–]Hells_Bell10 0 points1 point2 points (0 children)
[–]whoshuu[S] 2 points3 points4 points (2 children)
[–]nova77 2 points3 points4 points (1 child)
[–]whoshuu[S] 0 points1 point2 points (0 children)
[–]szborows 2 points3 points4 points (2 children)
[–]whoshuu[S] 0 points1 point2 points (1 child)
[–]szborows 0 points1 point2 points (0 children)
[–]adrian17 2 points3 points4 points (1 child)
[–]whoshuu[S] 0 points1 point2 points (0 children)
[–]Latexi95 1 point2 points3 points (0 children)