As far as I know, quite a bit of the ONNX export is implemented in Python.
So the two main options likely are:

Save the weights in C++, rebuild the module in Python, load the weights and export. Totally straightforward but a bit of effort.
You can export a TorchScript module to ONNX, too. So if the module is traceable, you should be able to do that to get a TorchScript module in C++. Then you can load that into Python and export. Given that ONNX export mostly does tracing internally, too, that might be a viable alternative that skips the “implement the model in Python” step.

Best regards
Thomas