Opened 6 years ago

Last modified 6 years ago

#12895 new Bugs

VS 2012: zip_iterator dereference causes a compiler error

Reported by: daniel.kruegler@… Owned by: jeffrey.hellrung
Milestone: To Be Determined Component: iterator
Version: Boost 1.63.0 Severity: Problem
Keywords: Cc:

Description

Using Visual Studio 2012 (vc110), any direct or indirect usage of the dereference operation of zip_iterator produces now a compiler error, a simple reproducer is the following example:

#include "boost/iterator/zip_iterator.hpp"

int main() 
{
    typedef boost::tuple<char*, char*> iterators_t;
    char chars[] = "abc";
    iterators_t iters(chars + 0, chars + 3);
    boost::iterators::zip_iterator<iterators_t> zip(iters);
    *zip;
}

which results in the following compiler output:

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2825: '_Iter': must be a class or namespace when followed by '::'
1>          c:\projects\thirdparty\boost_1_63_0\boost\iterator\iterator_traits.hpp(28) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\iterator\zip_iterator.hpp(90) : see reference to class template instantiation 'boost::iterators::iterator_reference<Iterator>' being compiled
1>          with
1>          [
1>              Iterator=char *const 
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\utility\result_of.hpp(190) : see reference to class template instantiation 'boost::iterators::detail::dereference_iterator::result<<unnamed-symbol>>' being compiled
1>          with
1>          [
1>              <unnamed-symbol>=boost::iterators::detail::dereference_iterator (char *const &)
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\utility\result_of.hpp(197) : see reference to class template instantiation 'boost::detail::result_of_nested_result<F,FArgs>' being compiled
1>          with
1>          [
1>              F=boost::iterators::detail::dereference_iterator,
1>              FArgs=boost::iterators::detail::dereference_iterator (char *const &)
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\utility\detail\result_of_iterate.hpp(37) : see reference to class template instantiation 'boost::detail::tr1_result_of_impl<F,FArgs,HasResultType>' being compiled
1>          with
1>          [
1>              F=boost::iterators::detail::dereference_iterator,
1>              FArgs=boost::iterators::detail::dereference_iterator (char *const &),
1>              HasResultType=false
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\utility\detail\result_of_iterate.hpp(160) : see reference to class template instantiation 'boost::tr1_result_of<F>' being compiled
1>          with
1>          [
1>              F=boost::iterators::detail::dereference_iterator (char *const &)
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\fusion\view\transform_view\detail\apply_transform_result.hpp(31) : see reference to class template instantiation 'boost::result_of<F>' being compiled
1>          with
1>          [
1>              F=boost::iterators::detail::dereference_iterator (char *const &)
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\mpl\aux_\preprocessed\plain\apply_wrap.hpp(39) : see reference to class template instantiation 'boost::fusion::detail::apply_transform_result<F>::apply<T0>' being compiled
1>          with
1>          [
1>              F=boost::iterators::detail::dereference_iterator,
1>              T0=char *const &
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\mpl\aux_\preprocessed\plain\apply.hpp(43) : see reference to class template instantiation 'boost::mpl::apply_wrap1<F,T1>' being compiled
1>          with
1>          [
1>              F=boost::fusion::detail::apply_transform_result<boost::iterators::detail::dereference_iterator>,
1>              T1=char *const &
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\mpl\aux_\preprocessed\plain\apply.hpp(51) : see reference to class template instantiation 'boost::mpl::apply1<F,T1>' being compiled
1>          with
1>          [
1>              F=boost::fusion::detail::apply_transform_result<boost::iterators::detail::dereference_iterator>,
1>              T1=char *const &
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\fusion\view\transform_view\detail\deref_impl.hpp(38) : see reference to class template instantiation 'boost::mpl::apply<F,T1>' being compiled
1>          with
1>          [
1>              F=boost::fusion::detail::apply_transform_result<boost::iterators::detail::dereference_iterator>,
1>              T1=char *const &
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\fusion\iterator\deref.hpp(54) : see reference to class template instantiation 'boost::fusion::extension::deref_impl<boost::fusion::transform_view_iterator_tag>::apply<Iterator>' being compiled
1>          with
1>          [
1>              Iterator=boost::fusion::transform_view_iterator<boost::fusion::boost_tuple_iterator<const iterators_t>,boost::iterators::detail::dereference_iterator>
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\fusion\adapted\boost_tuple\detail\build_cons.hpp(53) : see reference to class template instantiation 'boost::fusion::result_of::deref<Iterator>' being compiled
1>          with
1>          [
1>              Iterator=boost::fusion::transform_view_iterator<boost::fusion::boost_tuple_iterator<const iterators_t>,boost::iterators::detail::dereference_iterator>
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\fusion\adapted\boost_tuple\detail\build_cons.hpp(52) : while compiling class template member function 'boost::tuples::cons<HT,TT> boost::fusion::detail::build_tuple_cons<First,Last>::call(const First &,const Last &)'
1>          with
1>          [
1>              HT=char &,
1>              TT=boost::tuples::cons<char &,boost::fusion::detail::build_tuple_cons<boost::fusion::single_view_iterator<const boost::fusion::single_view<char &>,boost::mpl::int_<1>>,boost::fusion::single_view_iterator<const boost::fusion::single_view<char &>,boost::mpl::int_<1>>>::type>,
1>              First=boost::fusion::transform_view_iterator<boost::fusion::boost_tuple_iterator<const iterators_t>,boost::iterators::detail::dereference_iterator>,
1>              Last=boost::fusion::transform_view_iterator<boost::fusion::boost_tuple_iterator<const boost::tuples::null_type>,boost::iterators::detail::dereference_iterator>
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\fusion\adapted\boost_tuple\detail\convert_impl.hpp(43) : see reference to function template instantiation 'boost::tuples::cons<HT,TT> boost::fusion::detail::build_tuple_cons<First,Last>::call(const First &,const Last &)' being compiled
1>          with
1>          [
1>              HT=char &,
1>              TT=boost::tuples::cons<char &,boost::fusion::detail::build_tuple_cons<boost::fusion::single_view_iterator<const boost::fusion::single_view<char &>,boost::mpl::int_<1>>,boost::fusion::single_view_iterator<const boost::fusion::single_view<char &>,boost::mpl::int_<1>>>::type>,
1>              First=boost::fusion::transform_view_iterator<boost::fusion::boost_tuple_iterator<const iterators_t>,boost::iterators::detail::dereference_iterator>,
1>              Last=boost::fusion::transform_view_iterator<boost::fusion::boost_tuple_iterator<const boost::tuples::null_type>,boost::iterators::detail::dereference_iterator>
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\fusion\adapted\boost_tuple\detail\convert_impl.hpp(37) : see reference to class template instantiation 'boost::fusion::detail::build_tuple_cons<First,Last>' being compiled
1>          with
1>          [
1>              First=boost::fusion::transform_view_iterator<boost::fusion::boost_tuple_iterator<const iterators_t>,boost::iterators::detail::dereference_iterator>,
1>              Last=boost::fusion::transform_view_iterator<boost::fusion::boost_tuple_iterator<const boost::tuples::null_type>,boost::iterators::detail::dereference_iterator>
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\fusion\sequence\convert.hpp(37) : see reference to class template instantiation 'boost::fusion::extension::convert_impl<boost::fusion::boost_tuple_tag>::apply<Sequence>' being compiled
1>          with
1>          [
1>              Sequence=const boost::fusion::transform_view<const iterators_t,boost::iterators::detail::dereference_iterator>
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\iterator\zip_iterator.hpp(214) : see reference to class template instantiation 'boost::fusion::result_of::convert<Tag,Sequence>' being compiled
1>          with
1>          [
1>              Tag=tag,
1>              Sequence=const boost::fusion::transform_view<const iterators_t,boost::iterators::detail::dereference_iterator>
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\iterator\zip_iterator.hpp(289) : see reference to function template instantiation 'reference boost::iterators::detail::converter<reference>::call<boost::fusion::transform_view<Sequence1,Sequence2>>(Seq)' being compiled
1>          with
1>          [
1>              reference=reference,
1>              Sequence1=const iterators_t,
1>              Sequence2=boost::iterators::detail::dereference_iterator,
1>              Seq=boost::fusion::transform_view<const iterators_t,boost::iterators::detail::dereference_iterator>
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\iterator\zip_iterator.hpp(289) : see reference to function template instantiation 'reference boost::iterators::detail::converter<reference>::call<boost::fusion::transform_view<Sequence1,Sequence2>>(Seq)' being compiled
1>          with
1>          [
1>              reference=reference,
1>              Sequence1=const iterators_t,
1>              Sequence2=boost::iterators::detail::dereference_iterator,
1>              Seq=boost::fusion::transform_view<const iterators_t,boost::iterators::detail::dereference_iterator>
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\iterator\zip_iterator.hpp(284) : while compiling class template member function 'boost::tuples::cons<HT,TT> boost::iterators::zip_iterator<IteratorTuple>::dereference(void) const'
1>          with
1>          [
1>              HT=char &,
1>              TT=boost::tuples::cons<char &,boost::fusion::detail::build_tuple_cons<boost::fusion::single_view_iterator<const boost::fusion::single_view<char &>,boost::mpl::int_<1>>,boost::fusion::single_view_iterator<const boost::fusion::single_view<char &>,boost::mpl::int_<1>>>::type>,
1>              IteratorTuple=iterators_t
1>          ]
1>          c:\projects\thirdparty\boost_1_63_0\boost\iterator\iterator_facade.hpp(549) : see reference to function template instantiation 'boost::tuples::cons<HT,TT> boost::iterators::zip_iterator<IteratorTuple>::dereference(void) const' being compiled
1>          with
1>          [
1>              HT=char &,
1>              TT=boost::tuples::cons<char &,boost::fusion::detail::build_tuple_cons<boost::fusion::single_view_iterator<const boost::fusion::single_view<char &>,boost::mpl::int_<1>>,boost::fusion::single_view_iterator<const boost::fusion::single_view<char &>,boost::mpl::int_<1>>>::type>,
1>              IteratorTuple=iterators_t
1>          ]
1>          h:\develop\cpp\c++0x\vssandbox - 2011\main.cpp(8) : see reference to class template instantiation 'boost::iterators::zip_iterator<IteratorTuple>' being compiled
1>          with
1>          [
1>              IteratorTuple=iterators_t
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2039: 'iterator_category' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2146: syntax error : missing ';' before identifier 'iterator_category'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>'
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364) : see declaration of 'std::iterator_traits<_Iter>::iterator_category'
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2825: '_Iter': must be a class or namespace when followed by '::'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2039: 'value_type' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2146: syntax error : missing ';' before identifier 'value_type'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2602: 'std::iterator_traits<_Iter>::value_type' is not a member of a base class of 'std::iterator_traits<_Iter>'
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365) : see declaration of 'std::iterator_traits<_Iter>::value_type'
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2868: 'std::iterator_traits<_Iter>::value_type' : illegal syntax for using-declaration; expected qualified-name
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(366): error C2825: '_Iter': must be a class or namespace when followed by '::'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(366): error C2039: 'difference_type' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(366): error C2146: syntax error : missing ';' before identifier 'difference_type'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(366): error C2602: 'std::iterator_traits<_Iter>::difference_type' is not a member of a base class of 'std::iterator_traits<_Iter>'
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(366) : see declaration of 'std::iterator_traits<_Iter>::difference_type'
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(366): error C2868: 'std::iterator_traits<_Iter>::difference_type' : illegal syntax for using-declaration; expected qualified-name
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(368): error C2825: '_Iter': must be a class or namespace when followed by '::'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(368): error C2039: 'pointer' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(368): error C2146: syntax error : missing ';' before identifier 'pointer'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(368): error C2602: 'std::iterator_traits<_Iter>::pointer' is not a member of a base class of 'std::iterator_traits<_Iter>'
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(368) : see declaration of 'std::iterator_traits<_Iter>::pointer'
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(368): error C2868: 'std::iterator_traits<_Iter>::pointer' : illegal syntax for using-declaration; expected qualified-name
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(369): error C2825: '_Iter': must be a class or namespace when followed by '::'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(369): error C2039: 'reference' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(369): error C2146: syntax error : missing ';' before identifier 'reference'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(369): error C2602: 'std::iterator_traits<_Iter>::reference' is not a member of a base class of 'std::iterator_traits<_Iter>'
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(369) : see declaration of 'std::iterator_traits<_Iter>::reference'
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(369): error C2868: 'std::iterator_traits<_Iter>::reference' : illegal syntax for using-declaration; expected qualified-name
1>          with
1>          [
1>              _Iter=char *const 
1>          ]
1>c:\projects\thirdparty\boost_1_63_0\boost\fusion\adapted\boost_tuple\detail\build_cons.hpp(53): error C2440: 'initializing' : cannot convert from 'char *const ' to 'char &'
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

The problem seems rather severe to me and may occur even though the user-code didn't actually use zip_iterator directly. In fact I became aware of the problem when a colleague asked me why the following code would no longer be accepted when we switched from Boost 1.57 to Boost 1.63:

#include "boost/range/combine.hpp"

int main()
{
    int a[] = { 1, 2, 3 };
    int b[] = { 4, 5, 6 };
    
    for(const auto& p : boost::combine(a, b)) 
    {
    }
}

According to my not fully completed analysis, the problem is caused, because the current implementation of zip_iterator::dereference relies on a recently introduced feature of std::iterator_traits being now sfinae-friendly (See LWG issue 2408), which is not the case in the VS 2012 Standard Library. This assumption gets support by the fact that both VS 2015 and VS 2017 accept the code (And both compiler's Standard Libraries provide sfinae-friendly iterator_traits).

Change History (9)

comment:1 by anonymous, 6 years ago

Bisection among published Boost version shows that the first appearance of that problem occurred in Boost 1.60.

in reply to:  1 comment:2 by Michel Morin, 6 years ago

the first appearance of that problem occurred in Boost 1.60.

Maybe this PR is related.

comment:3 by daniel.kruegler@…, 6 years ago

The problem seems to be that boost::iterators::detail::dereference_iterator's member template result gets as argument Iterator the effective type char* const &, therefore the operation sequence

typedef typename
          remove_reference<typename remove_cv<Iterator>::type>::type
        iterator;

doesn't do enough, it just reduces Iterator to char* const, therefore std::iterator_traits<char* const> instantiates the primary template instead of the partial specialization for pointers.

Either one should prevent that a reference type is passed to this member template or the reduction code should be extended by adding an initial reference removal:

typedef typename
          remove_reference<typename remove_cv<typename
          remove_reference<Iterator>::type>::type>::type
        iterator;

in reply to:  3 ; comment:4 by daniel.kruegler@…, 6 years ago

Replying to daniel.kruegler@…:

Either one should prevent that a reference type is passed to this member template or the reduction code should be extended by adding an initial reference removal:

typedef typename
          remove_reference<typename remove_cv<typename
          remove_reference<Iterator>::type>::type>::type
        iterator;

In fact the application of remove_reference and remove_cv is applied in the wrong order, it would suffice to invert them as follows:

typedef typename remove_cv<typename
          remove_reference<Iterator>::type>::type
        iterator;

in reply to:  4 comment:5 by daniel.kruegler@…, 6 years ago

Replying to daniel.kruegler@…:

In fact the application of remove_reference and remove_cv is applied in the wrong order, it would suffice to invert them as follows:

typedef typename remove_cv<typename
          remove_reference<Iterator>::type>::type
        iterator;

I applied that simple fix locally and it was sufficient to resolve that problem. What is the preferred way to make a concrete patch suggestion?

comment:6 by Michel Morin, 6 years ago

Hi Daniel,

I failed to reproduce the error on gcc 6. Oh, I forgot that the default mode of gcc 6 is C++ 14, which means the decltype-based boost::result_of is used (by default).

If I explicitly specify -std=c++03 or define BOOST_RESULT_OF_USE_TR1, I can reproduce the error. I've also confirmed that the proposed fix resolves the compilation error.

What is the preferred way to make a concrete patch suggestion?

I think the best approach is to submit a pull request to https://github.com/boostorg/iterator . Thanks for the report and the fix!

in reply to:  6 comment:7 by daniel.kruegler@…, 6 years ago

Replying to michel:

Hi Daniel,

I failed to reproduce the error on gcc 6. Oh, I forgot that the default mode of gcc 6 is C++ 14, which means the decltype-based boost::result_of is used (by default).

If I explicitly specify -std=c++03 or define BOOST_RESULT_OF_USE_TR1, I can reproduce the error. I've also confirmed that the proposed fix resolves the compilation error.

Thanks for your independent evaluation and verification!

What is the preferred way to make a concrete patch suggestion?

I think the best approach is to submit a pull request to https://github.com/boostorg/iterator . Thanks for the report and the fix!

A pull request is available now:

https://github.com/boostorg/iterator/pull/22

comment:8 by Michel Morin, 6 years ago

A pull request is available now:

Great, thanks for submitting a PR. (PR 23 is a followup for it to add a testcase.)

comment:9 by Edward Diener, 6 years ago

I have applied the PR and the testcase PR in the latest 'develop' branch of iterator.

Note: See TracTickets for help on using tickets.