2

以下程序:

#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.Range 等算法boost::copy不适用于istreambuf_iterator.

这里发生了什么?我可以做些什么来修复它或解决它?

编辑:错误的直接原因似乎是istreambuf_iteratorreference_typechar&,但它的operator*回报char。对于格式良好的迭代器,不应该operator*总是返回reference_type吗?

4

1 回答 1

3

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

于 2011-08-09T23:40:42.413 回答