c++ - GCC 7.2 standard library possible constexpr bug in std::distance using the constexpr overload -


the function declared constexpr , std::distance has constexpr overload. when compile gcc7.2 error don't understand.

it seems me there bug in constexpr std::distance. error says calls std::__iterator_category(__first)); not constexpr.

live example

here code , error

#include <iostream> #include <algorithm> #include <iterator> #include <stdexcept> #include <array> #include <string> #include <cassert>  template<typename it> constexpr auto max (it first, last) {   auto count = std::distance(first, last);   if (count == 0)     throw std::domain_error{"max undefined on empty range"};   if (count == 1) return *first;   auto mid = first + (last - first) / 2;   return std::max(max(first, mid), max(mid, last)); }  int main() {   constexpr std::array<int, 4> vs{1,6,8,3};   constexpr auto max_of_vs = max(std::cbegin(vs), std::cend(vs));   assert(max_of_vs == 8);   std::cout << max_of_vs << std::endl;   std::array<int, 5> xs{1,9,8,3,6};   auto max_of_xs = max(std::begin(xs), std::end(xs));   assert(max_of_xs == 9);   std::cout << max_of_xs << std::endl;   //std::array<int, 0> zs{};   //auto max_of_zs = max(std::begin(zs), std::end(zs)); // throws @ runtime   //assert(max_of_zs == 0); } 

error:

main.cpp: in function 'int main()':  main.cpp:23:33:   in constexpr expansion of 'max<const int*>(std::cbegin<std::array<int, 4> >(vs), std::cend<std::array<int, 4> >(vs))'  main.cpp:12:29: error: 'constexpr typename std::iterator_traits<_iterator>::difference_type std::distance(_inputiterator, _inputiterator) [with _inputiterator = const int*; typename std::iterator_traits<_iterator>::difference_type = long int]' called in constant expression     auto count = std::distance(first, last);                  ~~~~~~~~~~~~~^~~~~~~~~~~~~  in file included /usr/local/include/c++/7.2.0/bits/stl_algobase.h:66:0,                   /usr/local/include/c++/7.2.0/bits/char_traits.h:39,                   /usr/local/include/c++/7.2.0/ios:40,                   /usr/local/include/c++/7.2.0/ostream:38,                   /usr/local/include/c++/7.2.0/iostream:39,                   main.cpp:1:  /usr/local/include/c++/7.2.0/bits/stl_iterator_base_funcs.h:138:5: note: 'constexpr typename std::iterator_traits<_iterator>::difference_type std::distance(_inputiterator, _inputiterator) [with _inputiterator = const int*; typename std::iterator_traits<_iterator>::difference_type = long int]' not usable constexpr function because:       distance(_inputiterator __first, _inputiterator __last)       ^~~~~~~~  /usr/local/include/c++/7.2.0/bits/stl_iterator_base_funcs.h:142:33: error: call non-constexpr function 'typename std::iterator_traits<_iterator>::iterator_category std::__iterator_category(const _iter&) [with _iter = const int*; typename std::iterator_traits<_iterator>::iterator_category = std::random_access_iterator_tag]'           std::__iterator_category(__first));           ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~ 


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -