为什么 std::istreambuf_iterator 无法通过 boost 的 SinglePassIterator 概念检查?
以下程序:
#include <boost/range/concepts.hpp>
#include <iterator>
#include <istream>
using boost::range_detail::SinglePassIteratorConcept;
int main()
{
BOOST_CONCEPT_ASSERT(( SinglePassIteratorConcept<std::istreambuf_iterator<char>> ));
}
无法使用 MSVC 和 gcc 进行编译。 MSVC 错误如下:
D:\libraries\boost\boost/range/concepts.hpp(157) : error C2440: 'initializing' : cannot convert from 'char' to 'char &'
D:\libraries\boost\boost/range/concepts.hpp(147) : while compiling class template member function 'boost::range_detail::SinglePassIteratorConcept<Iterator>::~SinglePassIteratorConcept(void)'
with
[
Iterator=std::istreambuf_iterator<char,std::char_traits<char>>
]
D:\libraries\boost\boost/concept/detail/has_constraints.hpp(42) : see reference to class template instantiation 'boost::range_detail::SinglePassIteratorConcept<Iterator>' being compiled
with
[
Iterator=std::istreambuf_iterator<char,std::char_traits<char>>
]
D:\libraries\boost\boost/concept/detail/msvc.hpp(58) : see reference to class template instantiation 'boost::concepts::not_satisfied<Model>' being compiled
with
[
Model=boost::range_detail::SinglePassIteratorConcept<std::istreambuf_iterator<char,std::char_traits<char>>>
]
test.cpp(10) : see reference to class template instantiation 'boost::concepts::require<Model>' being compiled
with
[
Model=boost::range_detail::SinglePassIteratorConcept<std::istreambuf_iterator<char,std::char_traits<char>>>
]
D:\libraries\boost\boost/range/concepts.hpp(160) : error C2440: 'initializing' : cannot convert from 'char' to 'char &'
因此,像 boost::copy
这样的 Boost.Range 算法无法与 istreambuf_iterator
一起使用。
这是怎么回事?我可以做些什么来修复它或解决它?
编辑:错误的直接原因似乎是istreambuf_iterator
的reference_type
是char&
,但它是operator*
返回char
。对于格式正确的迭代器,operator*
不应该总是返回 reference_type
吗?
The following program:
#include <boost/range/concepts.hpp>
#include <iterator>
#include <istream>
using boost::range_detail::SinglePassIteratorConcept;
int main()
{
BOOST_CONCEPT_ASSERT(( SinglePassIteratorConcept<std::istreambuf_iterator<char>> ));
}
Fails to compile with both MSVC and gcc. The MSVC error is as follows:
D:\libraries\boost\boost/range/concepts.hpp(157) : error C2440: 'initializing' : cannot convert from 'char' to 'char &'
D:\libraries\boost\boost/range/concepts.hpp(147) : while compiling class template member function 'boost::range_detail::SinglePassIteratorConcept<Iterator>::~SinglePassIteratorConcept(void)'
with
[
Iterator=std::istreambuf_iterator<char,std::char_traits<char>>
]
D:\libraries\boost\boost/concept/detail/has_constraints.hpp(42) : see reference to class template instantiation 'boost::range_detail::SinglePassIteratorConcept<Iterator>' being compiled
with
[
Iterator=std::istreambuf_iterator<char,std::char_traits<char>>
]
D:\libraries\boost\boost/concept/detail/msvc.hpp(58) : see reference to class template instantiation 'boost::concepts::not_satisfied<Model>' being compiled
with
[
Model=boost::range_detail::SinglePassIteratorConcept<std::istreambuf_iterator<char,std::char_traits<char>>>
]
test.cpp(10) : see reference to class template instantiation 'boost::concepts::require<Model>' being compiled
with
[
Model=boost::range_detail::SinglePassIteratorConcept<std::istreambuf_iterator<char,std::char_traits<char>>>
]
D:\libraries\boost\boost/range/concepts.hpp(160) : error C2440: 'initializing' : cannot convert from 'char' to 'char &'
As a result, Boost.Range algorithms like boost::copy
do not work with istreambuf_iterator
.
What is going on here? What can I do to fix it or work around it?
EDIT: The proximate cause of the error seems to be that istreambuf_iterator
's reference_type
is char&
, but it's operator*
returns char
. For a well-formed iterator, shouldn't operator*
always return reference_type
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
InputIterator
的operator*
类型的唯一要求是它可以转换为value_type
(§24.1.1/2)。由于为istreambuf_iterator
的operator*
结果赋值是没有意义的,因此它返回引用或任何类型的左值都是不正确的。 Boost 在检查该属性时出错。The only requirement of the type of
operator*
of anInputIterator
is that it be convertible tovalue_type
(§24.1.1/2). Since it's meaningless to assign a value to the result ofoperator*
for anistreambuf_iterator
, it would be incorrect for it to return a reference or any kind of lvalue. Boost is in error to check for that property.