3

I am trying to compile this code with clang 11.0.1, , for the PS4 platform. The some_array is std::array which is initialized as std::array<char, 48> some_array{{}};

auto* const characters = std::remove(some_array.begin(),
                        some_array.begin() + length, filtered_character);

and I get the following error

'auto *const' has incompatible initializer of type std::_Array_iterator<char, 48>

I am not sure if I need to suppress the warning using cmake or something to do with the logic.

JeJo
  • 30,635
  • 6
  • 49
  • 88

2 Answers2

5

The std::remove returns the forward iterator, not a pointer.

template< class ForwardIt, class T >
ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );
^^^^^^^^^^

In your case, it is of type

std::array<char, 48>::iterator

Therefore, you do not need to add the auto*, rather let the compiler deduce the type for you. So you need simply auto :

auto characters = std::remove(some_array.begin()
    , some_array.begin() + length
    , filtered_character);

In order to verify the type. see here

#include <type_traits> // std::is_same_v

auto characters = std::remove(some_array.begin()
    , some_array.begin() + length
    , filtered_character);

static_assert(std::is_same_v<decltype(characters), std::array<char, 48>::iterator>
    , "Are not same!");

As a side note, the algorithm std::remove will not actually remove any elements, but instead only move them to the end of the sequence, in such a way that, all "removed" elements fall behind a certain boundary in your range.

If your intention was to erase the elements, you should think of containers such as std::vector, std::list, etc with erase–remove idiom or simply std::erase(Since C++20). It is not possible with std::array

JeJo
  • 30,635
  • 6
  • 49
  • 88
4

It is not necessary that the iterator is defined as a pointer for the class template std::array. It is implementation defined (though some C++ libraries can indeed implement the iterator as a pointer).

And this error message

'auto *const' has incompatible initializer of type std::_Array_iterator<char, 48>

means that really in the used compiler and its libraries the iterator is not a pointer.

So use

auto characters = std::remove(some_array.begin(),
                  some_array.begin() + length, filtered_character);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335