llvm.org GIT mirror llvm / 653638b
Clarify struct usage guidelines The current coding standards restrict the use of struct to PODs, but no one has been following them. This patch updates the standards to clarify when structs are dangerous and describe common practice in LLVM. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202728 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 6 years ago
1 changed file(s) with 32 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
649649
650650 Unfortunately, not all compilers follow the rules and some will generate
651651 different symbols based on whether ``class`` or ``struct`` was used to declare
652 the symbol. This can lead to problems at link time.
653
654 So, the rule for LLVM is to always use the ``class`` keyword, unless **all**
655 members are public and the type is a C++ `POD
656 `_ type, in which case
657 ``struct`` is allowed.
652 the symbol (e.g., MSVC). This can lead to problems at link time.
653
654 * All declarations and definitions of a given ``class`` or ``struct`` must use
655 the same keyword. For example:
656
657 .. code-block:: c++
658
659 class Foo;
660
661 // Breaks mangling in MSVC.
662 struct Foo { int Data; };
663
664 * As a rule of thumb, ``struct`` should be kept to structures where *all*
665 members are declared public.
666
667 .. code-block:: c++
668
669 // Foo feels like a class... this is strange.
670 struct Foo {
671 private:
672 int Data;
673 public:
674 Foo() : Data(0) { }
675 int getData() const { return Data; }
676 void setData(int D) { Data = D; }
677 };
678
679 // Bar isn't POD, but it does look like a struct.
680 struct Bar {
681 int Data;
682 Foo() : Data(0) { }
683 };
658684
659685 Do not use Braced Initializer Lists to Call a Constructor
660686 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^