C++ syntactic pitfall: access modifiers as labels

  • although Java and C#’s approach to symbol privacy may be verbose, it has one great advantage: it is stateless.

    2023-09-07
  • the way they’re implemented in C++, it’s essentially a bit more parsing state you have to keep track of

    2023-09-07
    • and you know what other parsing state you have to keep track of in C++? - that’s right, the preprocessor.
      access modifiers, like all tokens, are affected by the preprocessor, and you have to take that into account.

      2023-09-07
      • take the following example:

        class ComfyZone
        {
            std::vector<SoftBed> _soft_beds;
        
        #if ENABLE_HUGS
        
        public:
            void hug(Person& person);
        
        #endif
        
            int _remaining_hugs = 10;
        };
        
        2023-09-07
        • although quite contrived, it illustrates the problem pretty well

          2023-09-07
          • (before you ask, _remaining_hugs needs to be always present because it has to be (de)serialized no matter if hugging functionality is compiled in. otherwise we’d get data loss.)

            2023-09-07
        • we intended for _remaining_hugs to be private, but if hugs are enabled, it becomes public.

          2023-09-07
          • this can be very hard to spot if you have a big class with lots of declarations inside.

            2023-09-07
      • this can be worked around by banning access modifiers from appearing in #ifdefs, but you have to realize that this might happen

        2023-09-07
        • and I’ve seen instances of this exact thing occurring in the Unreal Engine codebase, which is full of long lists of declarations (made even longer by the prevalence of UPROPERTY() specifiers)

          2023-09-07
  • even if we didn’t have the preprocessor, that access modifier is state you have to keep track of

    2023-09-07
    • I very often find myself needing to scroll upward after Ctrl-clicking on a field or function declaration, just to find out if I can use it

      2023-09-07
      • (thankfully IDEs are helpful here and Rider shows you a symbol’s visibility in the tooltip on hover, but I don’t have Rider on code reviews)

        2023-09-07