You may use the following:
const int64_t q = x / denominator;
const int64_t r = x - q * denominator;
// x = q * denominator + r;
const int64_t result = q * numerator + ((r * numerator) / denominator);
Note: you may get both the quotient and the remainder at once with std::div family.
Note: As pointed by Sander De Dycker, there are still problems when
r * numerator / denominator overflows
and with the special case x = INT64_MIN, denominator = -1 where x / denominator overflows.