What are the advantages of boost::noncopyable

前端 未结 11 1161
野性不改
野性不改 2020-11-27 14:10

To prevent copying a class, you can very easily declare a private copy constructor / assignment operators. But you can also inherit boost::noncopyable.

相关标签:
11条回答
  • 2020-11-27 14:31
    1. The intent of boost::noncopyable is clearer.
    2. Boost::noncopyable prevents the classes methods from accidentally using the private copy constructor.
    3. Less code with boost::noncopyable.
    0 讨论(0)
  • 2020-11-27 14:32

    It makes the intent explicit and clear, otherwise one has to see the definition of the class,and search for the declaration related to copy-semantic, and then look for the access-specifier in which it is declared, in order to determine whether the class is noncopyable or not. Other way to discover it by writing code that requires copy-semantic enabled and see the compilation error.

    0 讨论(0)
  • 2020-11-27 14:33

    The advantage is that you don't have to write a private copy constructor and a private copy operator yourself and it expresses clearly your intention without writing additional documentation.

    0 讨论(0)
  • 2020-11-27 14:36

    I'd rather use boost::noncopyable than manually delete or privatize the copy constructor and assignment operator.

    However, I almost never use either method, because:

    If I am making a non-copyable object, there has to be a reason it is non-copyable. This reason, 99% of the time, is because I have members that can't be copied meaningfully. Chances are, such members would also be better suited as private implementation details. So I make most such classes like this:

    struct Whatever {
      Whatever();
      ~Whatever();
      private:
      struct Detail;
      std::unique_ptr<Detail> detail;
    };
    

    So now, I have a private implementation struct, and since I've used std::unique_ptr, my top-level class is non-copyable for free. The link errors that come from this are understandable because they talk about how you can't copy a std::unique_ptr. To me, this is all the benefits of boost::noncopyable and a private implementation rolled into one.

    The benefit with this pattern is later, if I decide that I did indeed want to make my objects of this class copyable, I can just add and implement a copy constructor and/or assignment operator without changing the class hierarchy.

    0 讨论(0)
  • 2020-11-27 14:39

    Summarizing what others have said:

    Advantages of boost::noncopyable over private copy methods:

    1. It is more explicit and descriptive in the intent. Using private copy functions is an idiom that takes longer to spot than noncopyable.
    2. It is less code / less typing / less clutter / less room for error (the easiest would be accidentally providing an implementation).
    3. It embeds meaning right in the type's metadata, similar to a C# attribute. You can now write a function which accepts only objects which are noncopyable.
    4. It potentially catches errors earlier in the build process. The error will be presented at compile-time rather than link-time, in the case that the class itself or friends of the class are doing the erroneous copying.
    5. (almost the same as #4) Prevents the class itself or friends of the class from calling the private copy methods.

    Advantages of private copy methods over boost::noncopyable:

    1. No boost dependency
    0 讨论(0)
提交回复
热议问题