description | title | ms.date | ms.topic | f1_keywords | helpviewer_keywords | ms.assetid | ||
---|---|---|---|---|---|---|---|---|
Learn more about: C26437 DONT_SLICE |
C26437 |
10/07/2019 |
conceptual |
|
|
ed2f55bc-a6d8-4cc4-8069-5c96e581a96a |
"Do not slice."
C++ Core Guidelines: ES.63: Don't slice
Slicing is allowed by compiler and can be viewed as a special case of dangerous implicit cast. Even if it is done intentionally and doesn’t lead to immediate issues, it is still highly discouraged since it makes code rather unmaintainable by forcing additional requirements on related data types. This is especially true if types are polymorphic or involve resource management.
- This rule would warn not only on explicit assignments, but also on implicit slicing which happens when result gets returned from current function or data passed as arguments to other functions.
- Warnings would also flag cases where assignment doesn’t involve real data slicing (e.g. if types are empty or don’t make any dangerous data manipulations). Such warnings should still be addressed to prevent any undesirable regressions if types data or behavior changes in future.
slicing points to outdated
interface
struct id {
int value;
};
struct id_ex : id {
int extension;
};
bool read_id(stream &s, id &v) {
id_ex tmp{};
if (!s.read(tmp.value) || !s.read(tmp.extension))
return false;
v = tmp; // C26437
return true;
}
slicing points to outdated, interface - corrected
// ...
bool read_id(stream &s, id_ex &v) {
// ...