I would use shared pointer, something like this
void func(std::string optimizer_type) 
{
	std::shared_ptr<torch::optim::Optimizer> opt = nullptr;
	if (optimizer_type == "SGD") 
	{
		opt = std::make_shared<torch::optim::SGD>(model.parameters(), 0.01);
	}
	else 
	{
		opt = std::make_shared<torch::optim::Adam>(model.parameters());
	}
}