2

Here's my code

  void execute() {
    for (u32 f = 0; f < numForces; f++)
    {
      u32 length = end - start;
      PhysicalObject* arr = objects + start;
      std::vector<bool> tmp(length);
      bool* tmpArr = tmp.data();
      forces[f].affected(arr, length, tmpArr);
      for(u32 index = 0; index < length; index++)
        if (tmp[index])
          forces[f].apply(arr[index]);
    }
  }

the line

      bool* tmpArr = tmp.data();

throws me the error: "void value not ignored as it ought to be" I've looked into the documentation and I'm certain vector's data method is non void and should return a pointer: > http://www.cplusplus.com/reference/vector/vector/data/

Any ideas of what could be going wrong?

Important things:

  • I'm including vector
  • My compiling flags on gcc: -Wall -Wextra -Wcast-qual -pedantic -pedantic-errors -Wfatal-errors -Wno-missing-braces -Werror
  • the function is a class but I haven't added the rest as it just removes focus from the problem and it's a class made to run in parallel which uses a complex architecture.

2 Answers2

5

Unfortunately vector<bool> is special.

https://en.cppreference.com/w/cpp/container/vector_bool

You'd probably be better off switching to vector<uint8_t>

john
  • 85,011
  • 4
  • 57
  • 81
3

To briefly quote Scott Meyers from his Effective STL book, item 18: "Avoid using vector<bool>". The semantics of that specialization means that it is not completely conforming to the container requirements in the standard, because it doesn't actually store bools; it stores a packed representation using bits instead.

Whenever you need access to individual "bools" or you need a pointer to the data sequence (as in your example), you're in trouble, because there is no way to get a pointer to a single bit.

As mentioned by @john above, vector<uint8_t> may serve you well. Or, if you rewrite the function to use iterators instead of pointers, you could try with deque<bool>.

ravnsgaard
  • 922
  • 1
  • 10
  • 20