1.. title:: clang-tidy - cppcoreguidelines-avoid-const-or-ref-data-members
2
3cppcoreguidelines-avoid-const-or-ref-data-members
4=================================================
5
6This check warns when structs or classes that are copyable or movable, and have
7const-qualified or reference (lvalue or rvalue) data members. Having such
8members is rarely useful, and makes the class only copy-constructible but not
9copy-assignable.
10
11Examples:
12
13.. code-block:: c++
14
15  // Bad, const-qualified member
16  struct Const {
17    const int x;
18  }
19
20  // Good:
21  class Foo {
22   public:
23    int get() const { return x; }
24   private:
25    int x;
26  };
27
28  // Bad, lvalue reference member
29  struct Ref {
30    int& x;
31  };
32
33  // Good:
34  struct Foo {
35    int* x;
36    std::unique_ptr<int> x;
37    std::shared_ptr<int> x;
38    gsl::not_null<int*> x;
39  };
40
41  // Bad, rvalue reference member
42  struct RefRef {
43    int&& x;
44  };
45
46This check implements `C.12
47<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-constref>`_
48from the C++ Core Guidelines.
49
50Further reading:
51`Data members: Never const <https://quuxplusone.github.io/blog/2022/01/23/dont-const-all-the-things/#data-members-never-const>`_.
52