﻿# V2676\. MISRA\. The result of 'std::remove', 'std::remove\_if', 'std::unique' and 'empty' should be used\.

This diagnostic rule is based on the [MISRA](https://misra.org.uk/) \(Motor Industry Software Reliability Association\) software development guidelines\.

This diagnostic rule is relevant only for C\+\+\.

Calling certain functions does not make sense if their results are not used\. The result of calling the `[std::remove](https://timsong-cpp.github.io/cppwp/n4950/alg.remove)`, `[std::remove_if](https://timsong-cpp.github.io/cppwp/n4950/alg.remove)`, `[std::unique](https://timsong-cpp.github.io/cppwp/n4950/alg.unique)`, `[std::empty](https://timsong-cpp.github.io/cppwp/n4950/iterator.range#lib:empty(C&_c))` functions, or the `[empty](https://timsong-cpp.github.io/cppwp/n4950/container.reqmts#lib:empty,containers)` container member function should be used in the code\. For example, it can be passed to another function, stored in a variable for further logic, or checked in a condition\.

If the return value of these functions is not handled in any way, the code usually does not behave as expected\. In this case, the state of the objects or the data structure stays the same, which causes the program to behave incorrectly later on during execution\.

The first example:

```cpp
void remove_odd(std::vector<int> &v)
{
  std::remove_if(v.begin(), v.end(),
                 [](auto item)
                 { 
                   return (item & 1) != 0;
                 });
}
```

Calling the `std::remove_if` function, despite its name, does not remove elements from the vector\. It simply moves them within the vector so that the returned iterator points to the first element that satisfies the predicate\. The range of elements from this iterator to the end of the vector is marked for deletion\.

To complete the operation and remove the necessary elements from the vector, pass the returned iterator to the `[std:vector<int>::erase](https://timsong-cpp.github.io/cppwp/n4950/vector#lib:erase,vector)` member function:

```cpp
void remove_odd(std::vector<int> &v)
{
  auto it = std::remove_if(v.begin(), v.end(),
                           [](auto item)
                           {
                             return (item & 1) != 0;
                           });
  v.erase(it, v.end());
}
```

The second example:

```cpp
template <typename T>
void copy_elements(const std::vector<T> &src,
                         std::vector<T> &dst)
{
  dst.empty();
  dst.reserve(src.size());
  for (const T &elem : src)
  {
    dst.push_back(elem);
  }
}
```

The developer intended to clear the resulting vector before populating it\. However, calling `[std::vector<T>::empty](https://timsong-cpp.github.io/cppwp/n4950/container.reqmts#lib:empty,containers)` does not clear the container; it simply indicates whether it is empty\.

To clear the vector, use the `[std:vector<T>::clear](https://timsong-cpp.github.io/cppwp/n4950/sequence.reqmts#lib:clear,containers)` member function:

```cpp
template <typename T>
void copy_elements(const std::vector<T> &src,
                         std::vector<T> &dst)
{
  dst.clear();
  dst.reserve(src.size());
  for (const T &elem : src)
  {
    dst.push_back(elem);
  }
}
```