std::valarray 和迭代器类型
由于 C++11 std::valarray
具有迭代器,通过 std::begin()
和 std::end()
接口提供。但是这些迭代器的类型是什么(以便我可以正确声明它们)?
下面的代码不会在编译时出现 no template named 'iterator' in 'valarray<_Tp>'
错误:
template <typename T>
class A {
private:
std::valarray<T> ar;
std::valarray<T>::iterator iter;
public:
A() : ar{}, iter{std::begin(ar)} {}
};
decltype
显示迭代器的类型是指向的指针的类型一个“valarray”元素。事实上,以下内容确实可以编译并且似乎工作正常:
template <typename T>
class A {
private:
std::valarray<T> ar;
T* iter;
public:
A() : ar{}, iter{std::begin(ar)} {}
};
我缺少什么?类中是否没有合适的迭代器类型用于声明?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类型未指定。
您可以使用 decltype:
或者,在可能的情况下(不是成员变量),您应该更喜欢类型推导:
The type is unspecified.
You can use decltype:
Or, in cases where that's possible (not member variables), you should prefer type deduction: