Closed
Bug 700910
Opened 13 years ago
Closed 13 years ago
Implement macro-based support for C++11 = delete deleted function syntax, in at least some compilers
Categories
(Core :: MFBT, defect)
Core
MFBT
Tracking
()
RESOLVED
FIXED
mozilla11
People
(Reporter: Waldo, Assigned: Waldo)
References
Details
(Keywords: dev-doc-complete)
Attachments
(1 file)
7.50 KB,
patch
|
cjones
:
review+
|
Details | Diff | Splinter Review |
This is the common idiom to prevent copy-construction and default-assignability in C++98:
struct C
{
private:
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]
Patch
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+
Assignee | ||
Comment 2•13 years ago
|
||
https://hg.mozilla.org/integration/mozilla-inbound/rev/f2184dfdda31
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
Assignee | ||
Comment 3•13 years ago
|
||
I blogged about this:
http://whereswalden.com/2011/11/09/introducing-moz_delete-a-macro-improving-upon-the-deliberately-unimplemented-method-idiom/
And I posted to m.d.platform about it:
http://groups.google.com/group/mozilla.dev.platform/browse_thread/thread/c8319804e98df69f#
Updated•13 years ago
|
Keywords: dev-doc-needed
Comment 4•13 years ago
|
||
Status: ASSIGNED → RESOLVED
Closed: 13 years ago
Resolution: --- → FIXED
Assignee | ||
Updated•13 years ago
|
Component: General → MFBT
QA Contact: general → mfbt
Assignee | ||
Comment 5•13 years ago
|
||
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:
https://developer.mozilla.org/en/mfbt
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.
Description
•