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.
| クラス名 | Non-const Predicate Function Object | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 日本語クラス名 | Non-const Predicate Function Object | |||||||||
| クラス分類 | スタイル (style) | |||||||||
| ニーモニック | LANG.TYPE.NCPFO | |||||||||
| カテゴリー |
|
|||||||||
| 対応言語 | C++ のみ利用可能です。 C は利用できません。 |
|||||||||
| 有効/無効設定 | このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST
が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと
RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
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();
}
設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。
To report problems with this documentation, please visit https://support.codesecure.com/.