JDK-8234779 : Provide idiom for declaring classes noncopyable
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 14
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2019-11-25
  • Updated: 2022-06-27
  • Resolved: 2019-12-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 13 JDK 14
11.0.8-oracleFixed 13.0.7Fixed 14 b26Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
There have been occasional bugs arising from inappropriate use of default copy constructors or assignment operators.  These often arise through a failure to follow the Rule of 3, e.g. if a class needs any of a non-trivial destructor, copy ctor, or assignment operator, it needs all three.

A common case is a class that should never be copied, for semantic reasons.  It would be helpful to have a simple idiom to support that case.  This would make it obvious the Rule of 3 is followed for the class, and help make cases where it isn't stand out a bit more so they get additional scrutiny.

The suggested solution for the noncopyable case is to provide the following macro:

// Declare the named class to be noncopyable.  This macro must be used in
// a private part of the class's definition.  Doing so provides private
// declarations for the class's copy constructor and assignment operator.
// Because these operations are private, most potential callers will fail
// to compile because they are inaccessible.  The operations intentionally
// lack a definition, to provoke link-time failures for calls from contexts
// where they are accessible, e.g. from within the class or from a friend
// of the class.
// Note: The lack of definitions is still not completely bullet-proof,
// as an apparent call might be optimized away by copy elision.
// For C++11 the declarations should be changed to deleted definitions.
#define NONCOPYABLE(C) C(C const&); C& operator=(C const&);

Comments
Fix request (13u) I'd like to backport this fix to jdk13u for parity with jdk11u. The original patch applied not cleanly. The merge was required for several files. All regular tests passed. RFR: http://mail.openjdk.java.net/pipermail/jdk-updates-dev/2021-March/005405.html
22-03-2021

jdk11 backport request I would like to have the patch in OpenJDK 11 as well (for better parity with 11.0.8_oracle). The patch needs some adjustments and a few files touched by the jdk/jdk-change are not in 11 . RFR is here : https://mail.openjdk.java.net/pipermail/jdk-updates-dev/2020-May/003083.html .
12-05-2020

URL: https://hg.openjdk.java.net/jdk/jdk/rev/68f9bbda438d User: kbarrett Date: 2019-12-04 00:10:39 +0000
04-12-2019