diff options
author | Yao Qi <yao.qi@linaro.org> | 2017-09-15 15:40:50 +0000 |
---|---|---|
committer | Yao Qi <qiyao@gcc.gnu.org> | 2017-09-15 15:40:50 +0000 |
commit | 681f5b7cc8e80ef09f4692fac5bf3824e4854d54 (patch) | |
tree | dfc46c853b094c05f63c142a46308c2f86a5503d /include | |
parent | efae2b2f9b70196b6e282a2cebb97b6f00714a36 (diff) | |
download | gcc-681f5b7cc8e80ef09f4692fac5bf3824e4854d54.tar.gz |
[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.
Co-Authored-By: Pedro Alves <palves@redhat.com>
From-SVN: r252823
Diffstat (limited to 'include')
-rw-r--r-- | include/ChangeLog | 5 | ||||
-rw-r--r-- | include/ansidecl.h | 26 |
2 files changed, 31 insertions, 0 deletions
diff --git a/include/ChangeLog b/include/ChangeLog index 47035883d3b..0221586d778 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 ab3b895a475..450ce35da4b 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 |