samedi 26 février 2022

std::valarray and type of iterators

Since C++11 std::valarray has iterators, provided through the std::begin() and std::end() interfaces. But what is the type of those iterators (so that I can declare them properly)?

The following does not compile with a no template named 'iterator' in 'valarray<_Tp>' error:

template <typename T>
class A {
private:
  std::valarray<T> ar;
  std::valarray<T>::iterator iter;
public:
  A() : ar{}, iter{std::begin(ar)} {}
};

decltype shows the type of the iterator to be that of a pointer to a ``valarray` element. Indeed, the following does compile and seems to work fine:

template <typename T>
class A {
private:
  std::valarray<T> ar;
  T* iter;
public:
  A() : ar{}, iter{std::begin(ar)} {}
};

What am I missing? Isn't there a proper iterator type to use for the declare in the class?

Aucun commentaire:

Enregistrer un commentaire