-1
T offset = ps->first, prev_offset;
bool first = true;

while (1) {
  if (first)
    first = false;
  else
    assert(offset > prev_offset);
  pl = l.find_inc(offset);
  prev_offset = offset;
  if (pl == l.m.end())
    break;
  while (ps != s.m.end() && ps->first + ps->second <= pl->first)
    ++ps;
  if (ps == s.m.end())
    break;
  offset = pl->first + pl->second;
  if (offset <= ps->first) {
    offset = ps->first;
    continue;
  }
}

// I got [-Wunused-but-set-variable] warning of "prev_offset", is there any better way to solve it except adding cout << prev_offset; after prev_offset = offset; ? Any answer is appreciate, thanks in advance.

bigbin
  • 13
  • 1
  • 3
  • What's the data type of prev_offset? If you look at the first line, it's not initialized but you are using it here: assert(offset > prev_offset); – Asesh Sep 09 '17 at 07:32
  • 4
    Just remove it. And read up on `assert`. – juanchopanza Sep 09 '17 at 07:32
  • 3
    Possible duplicate of [How do I best silence a warning about unused variables?](https://stackoverflow.com/questions/1486904/how-do-i-best-silence-a-warning-about-unused-variables) – jpo38 Sep 09 '17 at 07:34
  • @Asseh The first loop iteration, it's not used. Every subsequent loop iteration, it's been assigned a value by the previous iteration. –  Sep 09 '17 at 07:44

1 Answers1

2

There are a couple of ways you can deal with this. One is to put in a dummy statement that casts the variable to (void).

(void) prev_offset; // this should stop the warning.

Another is to conditionally include the variable in a similar way to how assert() conditionally includes its check based on whether or not the NDEBUG macro is set.

// When you use ASSERT_CODE() the code only appears in debug builds
// the same as the assert() checks. That code is removed in 
// release builds.

#ifndef NDEBUG
#define ASSERT_CODE(code) code
#else
#define ASSERT_CODE(code)
#endif

// ...


T offset = ps->first;
ASSERT_CODE(T prev_offset); // give it its own line
bool first = true;

while (1) {
  if (first)
    first = false;
  else
    assert(offset > prev_offset);
  pl = l.find_inc(offset);

  ASSERT_CODE(prev_offset = offset); 

  if (pl == l.m.end())
    break;
  while (ps != s.m.end() && ps->first + ps->second <= pl->first)
    ++ps;
  if (ps == s.m.end())
    break;
  offset = pl->first + pl->second;
  if (offset <= ps->first) {
    offset = ps->first;
    continue;
  }
}
Galik
  • 47,303
  • 4
  • 80
  • 117