Noncopyable versus using delete
If you go looking for discussions of using boost::noncopyable versus using = delete; on the web, most of what you will find is a little beside the point. Much of it dates from shortly after the introduction of C++11 and mainly references the advantages of the new language capability afforded by delete.
To set out the considerations:delete:
- Part of the language and thus of the compiler
- Can be applied selectively, though any deletion of one copy operation without the other should prompt a look at your design
- Introduces no additional compile-time dependencies
- Should be understood by any C++ developer who is familiar with the language beyond bare competency
boost::noncopyable
- Requires boost to be available at compile time; this is not universal
- Can be used in C++03 codebases (yes, they still exist)
- Disables both copy operations, so not selective
- Requires additional knowledge in a developer beyond basic language knowledge
- Given that knowledge, it's a clearer expression of intent.
The last point seems to me to be important: there is a slight gain in clarity between "I am deleting these two operations" and "I intend this class as a whole to be noncopyable". Both are equally succinct (two lines of code) but the noncopyable approach puts that into the initial declaration of the class where it is highlighted, and will be picked up in high-level documentation of the class.
Comments
Post a Comment