#include <stdexcept>
#include <iostream>
#include <limits>

using namespace std;

// Check if an unsigned value could be the
// result of a bad conversion between a signed
// and an unsigned integer.
template <typename Signed, typename Unsigned>
bool wrap_from_negative(Unsigned i) {
    return static_cast<Signed>(i) > numeric_limits<Signed>::max();
}

void f(unsigned int i) {
    if (wrap_from_negative<int>(i))
        throw runtime_error("negative value");

    cout << i << endl;
}

int main() {

    f( 1);  // Ok.
    f(-1);  // Throws a runtime_error.

    return 0;
}