Skip to content

Latest commit

 

History

History
63 lines (53 loc) · 1.77 KB

File metadata and controls

63 lines (53 loc) · 1.77 KB
title ms.custom ms.date ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic f1_keywords helpviewer_keywords ms.assetid author ms.author manager
C26437 | Microsoft Docs
11/15/2017
vs-ide-code-analysis
article
C26437
C26437
ed2f55bc-a6d8-4cc4-8069-5c96e581a96a
mikeblome
mblome
ghogen

C26437 DONT_SLICE

"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.

Remarks

  • 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.

Example

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;
}

Example

slicing points to outdated

interface - corrected
// ...
bool read_id(stream &s, id_ex &v) {
// ...