1

I want to create a base class both noncopyable and nonmovable.

class noncopyable {
protected:
  noncopyable() = default;
  ~noncopyable() = default;

  noncopyable(noncopyable const &) = delete;
  noncopyable &operator=(noncopyable const &) = delete;
};

class nonmovable {
protected:
  nonmovable() = default;
  ~nonmovable() = default;

  nonmovable(nonmovable &&) = delete;
  nonmovable &operator=(nonmovable &&) = delete;
};

Is there any scenario that a class should be both noncopyable and nonmovable?

class non : public noncopyable, public nonmovable {
};

class foo : public non {
};

If there, what should be the convenient name for the "non" class here?

7
  • 4
    "is that convenient?" not really. Commented Mar 2, 2016 at 9:51
  • 2
    Sure, it can be quite useful. But you can also use boost::noncopyable. And usually you don't need public inheritance. And the deleted stuff can be private. Commented Mar 2, 2016 at 9:52
  • 6
    Your nonmovable is exactly the same as your noncopyable. What is it supposed to actually look like? Commented Mar 2, 2016 at 9:56
  • 2
    What do you define as "convenient"? For a class that should not be copied or moved, your approach may be useful. Also, do you intend that your noncopyable and nonmoveable are essentially the same, or is that a typo?
    – Peter
    Commented Mar 2, 2016 at 9:57
  • 2
    I find that abusing inheritance for this is actually more complicated then just deleting the appropriate member functions. You need to be aware of the rule of 5/0 anyways.
    – nwp
    Commented Mar 2, 2016 at 11:08

3 Answers 3

6

While a "noncopyable" will work, an "nonmovable" base class will not provide what you expect:

#include <utility>
#include <iostream>

struct nonmovable
{
    nonmovable() = default;
    nonmovable(const nonmovable&) { std::cout << "copy\n"; }
    nonmovable& operator = (const nonmovable&) { std::cout << "asign\n"; return *this; }
    nonmovable(nonmovable&&) = delete;
    nonmovable& operator = (nonmovable&&)  = delete;
};

struct X : nonmovable {};

int main()
{
    nonmovable n0;
    nonmovable n1(n0);
    // error: use of deleted function ‘nonmovable::nonmovable(nonmovable&&)’:
    //nonmovable n2(std::move(n0));

    X x0;
    X x1(x0);
    // However, X has a copy constructor not applying a move.
    X x2(std::move(x0));
}

In addition, move construction and move assignment must be enabled explicitly after deletion of the copy constructor, if desiered:

struct noncopyable
{
    noncopyable() = default;
    // Deletion of copy constructor and copy assignment makes the class
    // non-movable, too.
    noncopyable(const noncopyable&) = delete;
    noncopyable& operator = (const noncopyable&) = delete;

    // Move construction and move assignment must be enabled explicitly, if desiered.
    noncopyable(noncopyable&&) = default;
    noncopyable& operator = (noncopyable&&)  = default;
};

The, names "noncopyable" and "nonmovable" itself are good descriptive names. However, "boost::noncopyable" is both (non copyable and non movable), which might be a better (historical) design decision.

2
  • does "X x2(std::move(x0));" call move constructor or copy constructer. If the copy constructor is called. I reached my aim. Commented Mar 2, 2016 at 11:26
  • @mustafagonul the copy constructor of X (X has no move constructor, whatsoever)
    – user2249683
    Commented Mar 2, 2016 at 11:29
6

It has been argued, for instance here, that nonmoveable as demonstrated is a bad idea in the first place.

There are four reasonable options:

  1. Type is naturally copyable and moveable.
  2. Type is non-copyable, but moveable.
    This happens often if it is managing a resource, and copying the resource is impossible, or so expensive as to be undesirable.
  3. Type has no special moveability -- move ctor simply calls copy ctor.
    This mostly happens if for technical reasons, you don't want to create an "empty state" for objects of this type, or there is no cheap / natural way to leave the moved-from object in any acceptable state.
  4. Type is neither copyable nor moveable.
    This may happen with a static singleton or something like this, where moving or copying it is something that should never happen in your program and you want to prevent from happening with a compile-time error.

If the type is copyable, but moving it fails, this is strictly a bad arrangement, and instead move should be made to fallback to copy. There's no advantage to making a move fail when a copy would be okay, it only inhibits generic programming.

So maybe you should only have "noncopyable" and "non", but not "nonmoveable" ?

-3

as an example - signleton pattern. Also, if u defined copy-constructor/assignment operator/destructor, move-constructor/assignment wont be generated.

7
  • not really. take a look at the mutex class in standard C++
    – David Haim
    Commented Mar 2, 2016 at 11:00
  • And? mutex is movable by standard. It's up to vendor, how to implement it.
    – jonezq
    Commented Mar 2, 2016 at 11:02
  • 2
    @jonezq This disagrees. A std::mutex is not movable by the standard.
    – nwp
    Commented Mar 2, 2016 at 11:04
  • supposed to write not movable, yes. I meant, standard says nothing about design patterns. As an example msvc2012: _Mutex_base (base class for all mutex types) - isn't a singleton.
    – jonezq
    Commented Mar 2, 2016 at 11:07
  • 1
    The point is that non -movable/non-copiable is not the same as singletone. sometimes you want many instances of the same class but without the ability to move them, like mutexes. this is not singletone where you want just one instance
    – David Haim
    Commented Mar 2, 2016 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.