[libcxx-commits] [libcxx] [libcxx] Added segmented iterator for count_if (PR #105888)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 5 10:03:29 PST 2024


adeel10x wrote:

@philnik777 I tried using `auto` and function object like this:

```
template <class _InputIterator, class _Predicate>
struct __count_if{
	typename iterator_traits<_InputIterator>::difference_type __r;
	_Predicate __pred;
	_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __count_if(typename iterator_traits<_InputIterator>::difference_type& r, _Predicate& pred): __r(r), __pred(pred) {}
	_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR void operator()(const auto& __val) {
	  if (__pred(__val))
    	    ++__r;
  }
};


template <class _InputIterator, class _Predicate>
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
    typename iterator_traits<_InputIterator>::difference_type
    count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
  typename iterator_traits<_InputIterator>::difference_type __r(0);
  std::for_each(__first, __last, __count_if<_InputIterator, _Predicate>(__r, __pred));
  return __r;
}
```
It also causes c++ std version issue while compiling cxx-benchmarks. This is the error message:

 ```FAILED: CMakeFiles/cmTC_a2c87.dir/posix_regex.cpp.o 
/home/adeel/llvm-project/build/bin/clang++   -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/llvm-project/build2/include/c++/v1 -L/home/adeel/llvm-project/build2/lib -Wl,-rpath,/home/adeel/llvm-project/build2/lib  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -stdlib=libc++  -std=c++11 -MD -MT CMakeFiles/cmTC_a2c87.dir/posix_regex.cpp.o -MF CMakeFiles/cmTC_a2c87.dir/posix_regex.cpp.o.d -o CMakeFiles/cmTC_a2c87.dir/posix_regex.cpp.o -c /home/adeel/llvm-project/third-party/benchmark/cmake/posix_regex.cpp
In file included from /home/adeel/llvm-project/third-party/benchmark/cmake/posix_regex.cpp:2:
In file included from /home/adeel/llvm-project/build2/include/c++/v1/string:648:
In file included from /home/adeel/llvm-project/build2/include/c++/v1/string_view:959:
In file included from /home/adeel/llvm-project/build2/include/c++/v1/algorithm:1841:
/home/adeel/llvm-project/build2/include/c++/v1/__algorithm/count_if.h:28:64: error: 'auto' not allowed in function prototype
   28 |         _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR void operator()(const auto& __val) {
      |                                                                       ^~~~
1 error generated.
ninja: build stopped: subcommand failed.
```

Auto cannot be used in function parameters for >C++20. See this for [reference](https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template:~:text=Abbreviated%20function%20template).



https://github.com/llvm/llvm-project/pull/105888


More information about the libcxx-commits mailing list