为什么 std::istreambuf_iterator 无法通过 boost 的 SinglePassIterator 概念检查?

发布于 2024-11-28 22:08:21 字数 2273 浏览 0 评论 0原文

以下程序:

#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_iteratorreference_typechar&,但它是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉栀 2024-12-05 22:08:21

InputIteratoroperator* 类型的唯一要求是它可以转换为 value_type (§24.1.1/2)。由于为 istreambuf_iteratoroperator* 结果赋值是没有意义的,因此它返回引用或任何类型的左值都是不正确的。 Boost 在检查该属性时出错。

The only requirement of the type of operator* of an InputIterator is that it be convertible to value_type (§24.1.1/2). Since it's meaningless to assign a value to the result of operator* for an istreambuf_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文