52 lines · plain
1.. title:: clang-tidy - cppcoreguidelines-avoid-const-or-ref-data-members2 3cppcoreguidelines-avoid-const-or-ref-data-members4=================================================5 6This check warns when structs or classes that are copyable or movable, and have7const-qualified or reference (lvalue or rvalue) data members. Having such8members is rarely useful, and makes the class only copy-constructible but not9copy-assignable.10 11Examples:12 13.. code-block:: c++14 15 // Bad, const-qualified member16 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 member29 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 member42 struct RefRef {43 int&& x;44 };45 46This check implements `C.1247<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