# STL UnaryPredicates with TCollection

**URL:** <https://root-forum.cern.ch/t/stl-unarypredicates-with-tcollection/44159>\
**Category:** Newbie\
**Created:** [March 25, 2021, 11:36am UTC](https://root-forum.cern.ch/t/stl-unarypredicates-with-tcollection/44159 "2021-03-25T11:36:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![renyhp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@renyhp](https://root-forum.cern.ch/u/renyhp)\
**Post date:** [March 25, 2021, 11:36am UTC](https://root-forum.cern.ch/t/stl-unarypredicates-with-tcollection/44159/1 "2021-03-25T11:36:59Z")

</div>

Does ROOT not support STL’s UnaryPredicates, or am I doing something wrong? Thanks for your help!

```nohighlight
root [0] TObjArray *arr = new TObjArray()
root [1] all_of(arr->begin(), arr->end(), [](TObject *obj) { return true; })
In module 'std' imported from input_line_1:1:
/usr/include/c++/9/bits/stl_algo.h:173:8: error: no matching function for call to '__iterator_category'
                            std::__iterator_category(__first));
                            ^ ~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/bits/stl_algo.h:566:19: note: in instantiation of function template specialization 'std:: __find_if_not<TIter,__ gnu_cxx::__ops::_Iter_pred<(lambda at
      ROOT_prompt_1:1:34)> >' requested here
      return std::__find_if_not(__first, __last,
                  ^
/usr/include/c++/9/bits/stl_algo.h:509:29: note: in instantiation of function template specialization 'std::find_if_not<TIter, (lambda at ROOT_prompt_1:1:34)>' requested here
    { return __last == std::find_if_not(__first, __last,__ pred); }
                            ^
ROOT_prompt_1:1:1: note: in instantiation of function template specialization 'std::all_of<TIter, (lambda at ROOT_prompt_1:1:34)>' requested here
all_of(arr->begin(), arr->end(), [](TObject *obj) { return true; })
^
/usr/include/c++/9/bits/stl_iterator_base_types.h:205:5: note: candidate template ignored: substitution failure [with _Iter = TIter]: no type named 'iterator_category' in
      'std::iterator_traits<TIter>'
    __iterator_category(const _Iter&)
    ^

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [March 25, 2021, 10:11pm UTC](https://root-forum.cern.ch/t/stl-unarypredicates-with-tcollection/44159/2 "2021-03-25T22:11:40Z")

</div>

It does work on some platforms and not others. It looks like the C++ std libraries you are using here as “additional/new” requirements for containers that we are not yet added (it seems to be that we are missing `std::iterator_traits<TIter>`)

If you can not switch to std::vector. You can use this work around for TObjArray:

```auto
(arr->GetEntries()>0) && all_of(&((*arr)[0]), &((*arr)[arr->GetEntries()-1]), [](TObject *obj) { return true; })

```

---

<div class="post-metadata">

**Author:** ![renyhp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@renyhp](https://root-forum.cern.ch/u/renyhp)\
**Post date:** [March 26, 2021, 7:10am UTC](https://root-forum.cern.ch/t/stl-unarypredicates-with-tcollection/44159/3 "2021-03-26T07:10:19Z")

</div>

Thanks, but I guess that will not check for the last element. There are definitely cleaner ways to test this but this is the first thing that came to my mind:

```nohighlight
root [0] TObjArray *arr = new TObjArray()
root [1] (*arr)[0] = new TH1F("h", "", 1, 0, 1)
root [2] (*arr)[1] = new TH1F("g", "", 1, 0, 1)
root [3] (arr->GetEntries()>0) && all_of(&((*arr)[0]), &((*arr)[arr->GetEntries()-1]), [](TObject *obj) { return obj->GetName() == TString("h"); })
(bool) true
 

```

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [March 26, 2021, 8:14am UTC](https://root-forum.cern.ch/t/stl-unarypredicates-with-tcollection/44159/4 "2021-03-26T08:14:35Z")

</div>

> [@pcanal](#):
>
> It does work on some platforms and not others.

This sounds like a good candidate for a bug report at [Issues · root-project/root · GitHub](https://github.com/root-project/root/issues) – however as Philippe mentioned we typically suggest to switch to STL containers if possible.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![renyhp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@renyhp](https://root-forum.cern.ch/u/renyhp)\
**Post date:** [March 27, 2021, 3:36pm UTC](https://root-forum.cern.ch/t/stl-unarypredicates-with-tcollection/44159/5 "2021-03-27T15:36:22Z")

</div>

In my specific case it is not my code, so it’s not possible to use STL containers for me.

I can file a bug report, but probably I don’t have the necessary knowledge to understand precisely what is the bug there…

In the meantime as a workaround I wrote my own function:

```cpp
#include <functional>

bool all_of(TCollection *collection, const std::function<bool(TObject *)> &predicate)
{
  bool result = true;
  for (auto object : *collection)
    if (!predicate(object)) {
      result = false;
      break;
    }
  return result;
}

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [April 6, 2021, 6:07pm UTC](https://root-forum.cern.ch/t/stl-unarypredicates-with-tcollection/44159/6 "2021-04-06T18:07:52Z")

</div>

> [@renyhp](#):
>
> but I guess that will not check for the last element.

Oups 😇, simple off by one error:

```auto
(arr->GetEntries()>0) && all_of(&((*arr)[0]), &((*arr)[arr->GetEntries()]), [](TObject *obj) { return obj->GetName() == TString("h"); })
(bool) false

```
