Qt-Autosar-A25.1.1¶
Non-static data members or captured values of predicate function objects that are state related to this object’s identity shall not be copied
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Generic algorithms available in the C++ Standard Library accept a predicate function object. The ISO/IEC 14882:2014 C++ Language Standard states that it is implementation-defined whether predicate function objects can be copied by the STL algorithms.
To prevent from unexpected results while using predicate function objects, any such object shall either:
- be passed to an STL algorithm wrapped as a std::reference_wrapper.
- implement a function call operator that is const and does not modify any data members or captured values that have a mutable specifier.
Example
//% $Id: A25-1-1.cpp 309784 2018-03-01 20:18:29Z michal.szczepankiewicz $
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
class ThirdElemPred : public std::unary_function<int, bool>
{
public:
ThirdElemPred() : timesCalled(0) {}
bool operator()(const int &) { return (++timesCalled) == 3; }
//non-compliant, non-const call operator that
//modifies the predicate object field
private:
size_t timesCalled;
};
class ThirdElemPred2 : public std::unary_function<int, bool>
{
public:
ThirdElemPred2() : timesCalled(0) {}
bool operator()(const int &) const { return (++timesCalled) == 3; }
//non-compliant, const call operator that
//modifies the mutable predicate object field
private:
mutable size_t timesCalled;
};
class ValueFivePred: public std::unary_function<int, bool>
{
public:
bool operator()(const int& v) const { return v == 5; }
//compliant, const call operator that does not
//modify the predicate object state
};
void F1(std::vector<int> v)
{
//non-compliant, predicate object state modified
int timesCalled = 0;
//display values that are NOT to be removed
std::copy(v.begin(), std::remove_if(v.begin(), v.end(), [timesCalled](constint &) mutable { return (++timesCalled) == 3; }), std::ostream_iterator<std::vector<int>::value_type>(std::cout, " ") );
std::cout << std::endl;
}
void F2(std::vector<int> v)
{
//non-compliant, predicate object state modified
std::copy(v.begin(), std::remove_if(v.begin(), v.end(), ThirdElemPred()), std::ostream_iterator<std::vector<int>::value_type>(std::cout, " ") );
std::cout << std::endl;
}
void F22(std::vector<int> v)
{
//non-compliant, predicate object state modified
std::copy(v.begin(), std::remove_if(v.begin(), v.end(), ThirdElemPred2()),std::ostream_iterator<std::vector<int>::value_type>(std::cout, " ") );
std::cout << std::endl;
}
void F3(std::vector<int> v)
{
//compliant, predicate object that has its state
//modified is passed as a std::reference_wrapper
ThirdElemPred pred;
std::copy(v.begin(), std::remove_if(v.begin(), v.end(), std::ref(pred)), std::ostream_iterator<std::vector<int>::value_type>(std::cout, " ") );
std::cout << std::endl;
}
int main(void)
{
std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
F1(v);
F2(v);
F22(v);
F3(v);
//output for g++-5.5, correct result only for F3
//F1 0 1 3 4 6 7 8 9
//F2 0 1 3 4 6 7 8 9
//F22 0 1 3 4 6 7 8 9
//F3 0 1 3 4 5 6 7 8 9
return 0;
}
See Also
- SEI CERT C++ Coding Standard [10]: CTR58-CPP: Predicate function objects should not be mutable
- cppreference.com [16]: C++ concepts: Predicate
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
modifies_state |
Call operator of predicate passed to {} modifies state. |
None |
False |
nonconst_op |
Call operator of predicate passed to {} is not const. |
None |
False |
Options
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.