JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
The function call operator of a user-defined predicate function object modifies internal object state.
A predicate function object P will only trigger a warning of this class if:
For the sake of this warning class, a predicate function object is an object of a class that overrides operator() to return bool or unsigned char.
| Class Name | Non-const Predicate Function Object | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Significance | style | |||||||||
| Mnemonic | LANG.TYPE.NCPFO | |||||||||
| Categories |
|
|||||||||
| Availability | Available for C++ only (not C). |
|||||||||
| Enabling | Checks for this warning class are disabled by default,
and require the unnormalized
C ASTs for the project. To enable them, add the following
WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes WARNING_FILTER += allow class="Non-const Predicate Function Object" |
#include <vector>
#include <algorithm>
class PfoNotWrapped {
private:
size_t timesCalled;
public:
PfoNotWrapped() : timesCalled(0) {}
bool operator() (const int &) { // 'Non-const Predicate Function Object' warning issued here
++timesCalled;
return timesCalled == 3;
}
};
class PfoWrapped {
private:
size_t timesCalled;
public:
PfoWrapped() : timesCalled(0) {}
bool operator() (const int &) { // ok: all uses are wrapped with std::reference_wrapper
++timesCalled;
return timesCalled == 3;
}
};
class PfoConst {
private:
size_t timesCalled;
public:
PfoConst() : timesCalled(0) {}
bool operator() (const int &) const { // ok: uses are not wrapped, but operator() does not modify object state
return timesCalled == 3;
}
};
size_t non_const_pred_func_object(void) {
std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
size_t timesCalled = 0;
auto it1 = std::remove_if(v.begin(),
v.end(),
[timesCalled](const int &) mutable { // 'Non-const Predicate Function Object' warning issued here
++timesCalled;
return timesCalled == 3;
});
PfoNotWrapped predNW;
auto it2 = std::remove_if(v.begin(),
it1,
predNW); // not in a std::reference_wrapper: causes warning to be issued for PfoNotWrapped::operator(), above
PfoWrapped predW;
auto it3 = std::remove_if(v.begin(),
it2,
std::ref(predW)); // std::ref() creates a std::reference_wrapper
PfoConst predC;
auto it4 = std::remove_if(v.begin(),
it3,
predC); // predC() does not mutate internal state so does not need to be wrapped
v.erase(it4, v.end());
return v.size();
}
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.