c – Clang vs gcc std :: crbegin with boost :: iterator_range

前端之家收集整理的这篇文章主要介绍了c – Clang vs gcc std :: crbegin with boost :: iterator_range前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
带有libc的Clang 3.8.1编译以下程序:
  1. #include <vector>
  2. #include <iterator>
  3. #include <algorithm>
  4. #include <iostream>
  5.  
  6. #include <boost/range/iterator_range.hpp>
  7.  
  8. int main()
  9. {
  10. const std::vector<int> v {1,2,3};
  11.  
  12. const auto range = boost::make_iterator_range(v);
  13.  
  14. std::copy(std::crbegin(range),std::crend(range),std::ostream_iterator<int> {std::cout," "});
  15. std::cout << std::endl;
  16.  
  17. return 0;
  18. }

但是使用libstdc的gcc 6.1.0却没有.第一行gcc错误是:

  1. error: no matching function for call to 'crbegin(const boost::iterator_range<__gnu_cxx::__normal_iterator<const int*,std::vector<int> > >&

谁是对的?

注意:Boost版本1.61

解决方法

这是一个 bug in libc++; std :: crbegin委托给rbegin,但是通过调用它不合格它正在拿起 boost::rbegin( documentation):
  1. template <class _Cp>
  2. inline _LIBCPP_INLINE_VISIBILITY
  3. auto crbegin(const _Cp& __c) -> decltype(rbegin(__c))
  4. {
  5. return rbegin(__c);
  6. // ^-- unqualified,allows ADL
  7. }

这与[iterator.range]相反,后者表示crbegin只应委托给std :: rbegin:

template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c));

14 – Returns: std::rbegin(c).

Libc的cbegin,cend和crend的实现也有同样的bug.

猜你在找的C&C++相关文章