Home > Enterprise >  std::valarray and type of iterators
std::valarray and type of iterators

Time:02-28

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?

CodePudding user response:

But what is the type of those iterators

The type is unspecified.

(so that I can declare them properly)?

You can use decltype:

using It = decltype(std::begin(ar));
It iter;

Or, in cases where that's possible (not member variables), you should prefer type deduction:

auto iter = std::begin(ar);
  • Related