| description | Learn more about: Warning C26437 DONT_SLICE | ||
|---|---|---|---|
| title | Warning C26437 | ||
| ms.date | 10/07/2019 | ||
| f1_keywords |
|
||
| helpviewer_keywords |
|
||
| ms.assetid | ed2f55bc-a6d8-4cc4-8069-5c96e581a96a |
Do not slice.
C++ Core Guidelines: ES.63: Don't slice
Slicing is allowed by the compiler and can be viewed as a special case of a dangerous implicit cast. Even if it's done intentionally and doesn't lead to immediate issues, it's still highly discouraged. It makes code unmaintainable, by forcing extra requirements on related data types. It's especially true if types are polymorphic or involve resource management.
This rule warns not only on explicit assignments, but also on implicit slicing. Implicit slicing happens when a result gets returned from the current function, or when data gets passed to other functions.
The rule also flags cases where an assignment doesn't involve real data slicing (for example, if types are empty or don't make any dangerous data manipulations). Such warnings should still be fixed to prevent any undesirable regressions if data types or behaviors change in the future.
Slicing points to outdated interface:
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) {
// ...