This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Add macro DISABLE_COPY_AND_ASSIGN
- From: Yao Qi <qiyaoltc at gmail dot com>
- To: Ian Lance Taylor <iant at golang dot org>
- Cc: Pedro Alves <palves at redhat dot com>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Fri, 15 Sep 2017 16:44:04 +0100
- Subject: Re: [PATCH] Add macro DISABLE_COPY_AND_ASSIGN
- Authentication-results: sourceware.org; auth=none
- References: <1500456645-30359-1-git-send-email-yao.qi@linaro.org> <20170726085510.GA8039@1170ee0b50d5> <CAH=s-POK+LNJLCo8LrMRBJmDyhrGFxsCL-XDGajJfevFn5MSOw@mail.gmail.com> <b6d3391f-5f0b-501e-551e-e03160076c18@redhat.com> <CAH=s-PM5x-L0vmBak7sO6tDYJyTSy0Lghn8jwKO3S0mgrnzTaA@mail.gmail.com> <CAKOQZ8y_9Ox8vd21rizYmcmcLAJi9m8CDg=yrLZMOnx+B+6yRg@mail.gmail.com>
On Sat, Sep 9, 2017 at 1:27 PM, Ian Lance Taylor <iant@golang.org> wrote:
>
> The patch to include/ansidecl.h is basically OK. Please add an
> example in the comment showing how to use it: after `private:`, and
> with a trailing semicolon. Thanks.
Patch below is committed. Thanks for the review.
>
> The patches to the other files will have to be approved by the
> relevant maintainers.
>
I'll split it and post them later.
--
Yao (齐尧)
From 753d12319d85876c2513029037af539c43717251 Mon Sep 17 00:00:00 2001
From: qiyao <qiyao@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Fri, 15 Sep 2017 15:40:50 +0000
Subject: [PATCH] [include] Add macro DISABLE_COPY_AND_ASSIGN
We have many classes that copy cotr and assignment operator are deleted
in different projects, gcc, gdb and gold. So this patch adds a macro
to do this, and replace these existing mechanical code with macro
DISABLE_COPY_AND_ASSIGN.
The patch was posted in gdb-patches,
https://sourceware.org/ml/gdb-patches/2017-07/msg00254.html but we
think it is better to put this macro in include/ansidecl.h so that
other projects can use it too.
include:
2017-09-15 Yao Qi <yao.qi@linaro.org>
Pedro Alves <palves@redhat.com>
* ansidecl.h (DISABLE_COPY_AND_ASSIGN): New macro.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@252823 138bc75d-0d04-0410-961f-82ee72b054a4
---
include/ChangeLog | 5 +++++
include/ansidecl.h | 26 ++++++++++++++++++++++++++
2 files changed, 31 insertions(+)
diff --git a/include/ChangeLog b/include/ChangeLog
index 4703588..0221586 100644
--- a/include/ChangeLog
+++ b/include/ChangeLog
@@ -1,3 +1,8 @@
+2017-09-15 Yao Qi <yao.qi@linaro.org>
+ Pedro Alves <palves@redhat.com>
+
+ * ansidecl.h (DISABLE_COPY_AND_ASSIGN): New macro.
+
2017-09-12 Jiong Wang <jiong.wang@arm.com>
* dwarf2.def (DW_CFA_AARCH64_negate_ra_state): New DW_CFA_DUP.
diff --git a/include/ansidecl.h b/include/ansidecl.h
index ab3b895..450ce35 100644
--- a/include/ansidecl.h
+++ b/include/ansidecl.h
@@ -360,6 +360,32 @@ So instead we use the macro below and test it against specific values. */
# define FINAL
#endif
+/* A macro to disable the copy constructor and assignment operator.
+ When building with C++11 and above, the methods are explicitly
+ deleted, causing a compile-time error if something tries to copy.
+ For C++03, this just declares the methods, causing a link-time
+ error if the methods end up called (assuming you don't
+ define them). For C++03, for best results, place the macro
+ under the private: access specifier, like this,
+
+ class name_lookup
+ {
+ private:
+ DISABLE_COPY_AND_ASSIGN (name_lookup);
+ };
+
+ so that most attempts at copy are caught at compile-time. */
+
+#if __cplusplus >= 201103
+#define DISABLE_COPY_AND_ASSIGN(TYPE) \
+ TYPE (const TYPE&) = delete; \
+ void operator= (const TYPE &) = delete
+ #else
+#define DISABLE_COPY_AND_ASSIGN(TYPE) \
+ TYPE (const TYPE&); \
+ void operator= (const TYPE &)
+#endif /* __cplusplus >= 201103 */
+
#ifdef __cplusplus
}
#endif
--
1.9.1