A good method is not to allow the error to compile!
Unfortunately, merely using unsigned will not prevent a negative value from being passed to the constructor – it will just be converted implicitly to an unsigned value, thus hiding the error (as Alf pointed out in a comment).
The “proper” solution would be to write a custom nonnegative (or more generally, constrained_value) type to make the compiler catch this error – but for some projects this may be too much overhead, since it can easily lead to a proliferation of types. Since it improves (compile-time) type safety, I still think that this is fundamentally the right approach.
A simple implementation of such a constrained type would look as follows:
struct nonnegative { nonnegative() = default; template <typename T, typename = typename std::enable_if<std::is_unsigned<T>::value>::type> nonnegative(T value) : value{value} {} operator unsigned () const { return value; } private: unsigned value; };
See it in action
This prevents construction from anything but an unsigned type. In other words, it simply disables the usual implicit, lossy conversion from signed to unsigned numbers.
If the error cannot be caught at compile time (because the value is received from the user), something like exceptions are the next best solution in most cases (although an alternative is to use an option type or something similar).
Often you would encapsulate this throwing of an exception in some kind of ASSERT macro. In any case, user input validation should not happen inside a class’ constructor but immediately after the value has been read from the outside world. In C++, the most common strategy is to rely on formatted input:
unsigned value; if (! (std::cin >> value)) // handle user input error, e.g. throw an exception.