The ex-premier said there was a danger that elections could be decided on questions not set by political leaders, with the electorate voting “on any number of other questions”.
Mr Cameron also urged Mr Johnson to obey the Benn Act, which obliges him to seek a Brexit delay if he cannot get a deal, saying “you can’t disobey the law as prime minister”.
The former Conservative leader, 52, was appearing on stage with Times editor John Witherow at the Barbican centre in London in front of a 2,000-strong audience.
Mr Witherow asked: “Do you think if there was a general election, would the Tories win with Corbyn there?”
Mr Cameron said: “I just don’t know. I fought an election in 2015 when I was told I was definitely going to lose, and I won. And I fought a referendum and I was told I was definitely going to win, and I lost.
“Strangely enough, I still look at opinion polls.
“The trouble with general elections is you’re asking one question, but people may easily go and vote on any number of other questions.
“I think that’s what happened in 2017.”
Mr Cameron added he backed the current Prime Minister’s attempts to secure a deal with Brussels and take it through the Commons.
He said: “The answer I suspect is now compromise on both sides.
“Boris is compromising and I suspect may have to move a bit more. And the EU should compromise too.”
The pair discussed aspects of Mr Cameron’s book For The Record, extracts of which were recently published in the Times and Sunday Times.
Mr Cameron said he regretted not fighting harder to demand caps on EU immigration, and said German Chancellor Angela Merkel always accused him of wanting “extra wurst” in his attempts to negotiate deals for Britain.
He also revealed that when he was exhausted on board Air Force One, former US president Barack Obama “tucked me in” to his own bed on the aircraft, saying: “I bet Roosevelt never did this for Churchill.”