Implement macro-based support for C++11 = delete deleted function syntax, in at least some compilers

RESOLVED FIXED in mozilla11



6 years ago
6 years ago


(Reporter: Waldo, Assigned: Waldo)




Firefox Tracking Flags

(Not tracked)



(1 attachment)

Created attachment 573062 [details] [diff] [review]

This is the common idiom to prevent copy-construction and default-assignability in C++98:

  struct C
      C(const C& other);
      void operator=(const C& other);

In other words, declare private versions of these methods but never implement them.  This is a bit unclear, because nothing prevents someone from later defining these methods.  (They could only be used by C or friends of C, to be sure, but for sufficiently complex code it's possible someone might make a mistake.)  C++11 formalizes and clarifies the trick by introducing deleted function syntax:

  struct C
    C(const C& other) = delete;
    void operator=(const C& other) = delete;

Besides its clarity, a further advantage to this syntax is that defining a deleted method out-of-line, or accidentally attempting to use either method, produces a compile error -- not just a link error or, worse yet, no error at all.

I've wrapped up support for deleted functions under a small MOZ_DELETE macro.  It's not as nice as = delete, but it's searchable, and the adjacent comment will clarify matters.  Unfortunately compile-time error behavior only works in clang, but that's an improvement over status quo.  And I lack the knowledge and time to get gcc working now.  :-\

I would do the check for -Wno-c++0x-extensions support the same way support for -Wno-invalid-offsetof is divined, but it turns out that approach is buggy.  gcc (at least 4.6) compiles successfully with unrecognized -Wno-* command line flags, so the only way to target clang only is to check for clang specifically.

I'm sure I could spatter this macro many more places, but it's not easy to search for the pattern, so changing things incrementally seems simplest.  I've made a few changes here only to verify that the macro works in both Gecko and SpiderMonkey.
Attachment #573062 - Flags: review?(jones.chris.g)
Comment on attachment 573062 [details] [diff] [review]

Good idea.

Could use __GXX_EXPERIMENTAL_CXX0X__ to enable this for gcc but it's probably not worth the effort.
Attachment #573062 - Flags: review?(jones.chris.g) → review+

I'll blog about this and probably note that it's only awesome on clang for the moment.  I'm sure somebody can make it work on gcc given the right incentives.  :-)
Target Milestone: --- → mozilla11
Blocks: 701183
I blogged about this:

And I posted to m.d.platform about it:
Keywords: dev-doc-needed
Last Resolved: 6 years ago
Resolution: --- → FIXED
Component: General → MFBT
QA Contact: general → mfbt
I consider the comments by these macros to be much better documentation than anything maintained externally, so I think this document roughly pointing people to it is reasonable documentation:

Note that this macro no longer lives in Util.h, rather in Attributes.h now, so that people have a better chance guessing where it's defined in mfbt/ based solely on the functionality desired.
Keywords: dev-doc-needed → dev-doc-complete
You need to log in before you can comment on or make changes to this bug.