简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM