Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Incorrect behavior of reversed_range with -O2 g++ option

Open
#82 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
tooling

Research direction

Start by compiling the example code from the issue with g++ or clang++ at -O0 and -O2, then inspect the reversed_range entry points used by boost::adaptors::reverse. Confirm that the optimized build produces the expected 2 1 0 output, and add a regression test covering the reported compilers and optimization level.

Written by the indexing model from the issue text.

Description

Example code:

#include "boost/iterator/counting_iterator.hpp"
#include "boost/range/iterator_range.hpp"
#include "boost/range/adaptor/reversed.hpp"

#include <iostream>

int main(int, char **)
{
    using iterator = boost::counting_iterator<int>;
    using iterator_range = boost::iterator_range<iterator>;
    using reversed_range = boost::reversed_range<iterator_range>;

    iterator first(0), last(3);
    iterator_range range(first, last);
    reversed_range r_range = boost::adaptors::reverse(range);

    reversed_range::const_iterator it = r_range.begin();
    for (; it != r_range.end(); ++it)
        std::cout << *it << " ";
    std::cout << std::endl;
}

Compile with: -O2

Output: 3 integer numbers, different from run to run, similar to

22007 22007 22007

(with g++)
or

-1807142200 0 0

(with clang++)

When compiled with -O0 produces the correct output:

2 1 0

Reproducible with:
Boost 1.67.0, 1.68.0
g++ 6.4.0, 7.3.0, 8.2.0; clang++ 7.0.0-3
CPU architecture: x86-64 (Intel Xeon)

Dominant language
C++
Stars
45
Forks
104
PR merge metrics
No merged PRs in 30d

Getting set up

We have not checked this project's setup files yet. Start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/range

All issues in boostorg/range

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.