President Obama’s evolutionary leap on same-sex marriage is a historic advance in the nation’s long march toward equality and justice. It is also a bold political gambit that sacrifices some votes in exchange for potentially renewing his image as a leader of vision and hope.

The truth is that it should not have taken Obama so long to recognize that gay men and lesbians should have the right to marry. I’m one of the many observers who never understood how his former opposition to same-sex marriage could be squared with the worldview that emerged from his speeches and actions. It seemed incongruous that someone who so valued fairness and inclusiveness would have such a blind spot.

Nor do I understand Obama’s criteria for deciding that his “evolving” view on gay marriage had completed its transformation. Was it only half-baked, say, a month ago?

Ultimately, history will care only that Obama was the first president to acknowledge that same-sex marriage is a national issue involving the civil rights of millions of Americans. The astonishment and joy expressed by so many gay people nationwide after Obama’s announcement Wednesday showed what a big deal this is.

We all know where this is heading. Obama said that although he supports same-sex marriage, the decision should be left up to the states. That would seem to bode ill, since 30 states have amended their constitutions to prohibit gay marriage; on Tuesday, North Carolina overwhelmingly approved such an amendment, with 61 percent of voters approving a ban on same-sex marriage.

But polls show that public opinion on gay marriage has been shifting rapidly across the country. A Washington Post survey in March reported that 52 percent of Americans think it should be legal for same-sex couples to marry; 43 percent think it should be illegal. In a March 2004 poll, The Post found that 38 percent of respondents thought gay marriage should be legal, with 59 percent opposed. That’s almost a complete reversal in eight years.

Moreover, polls show a clear generational divide: Americans younger than 40 approve of gay marriage by a big margin. This explains the rush to amend state constitutions in what amounts to a King Canute-like attempt to hold back the actuarial tide.

But same-sex marriage is already allowed in Connecticut, Iowa, Massachusetts, New Hampshire, New York, Vermont and the District. As more couples wed, courts across the country will have to rule on questions involving marriages that are recognized in some states but not others. It may be a long, tangled process, but eventually a day will come when same-sex marriage is considered unexceptional and only historians appreciate that once upon a time it was controversial.

Obama’s pronouncement hastens that day. It also has shorter-term implications.

It seems clear that his position on gay marriage will cost Obama some support in what promises to be a tough battle for reelection. The crucial impact will be in the swing states. North Carolina, for example, is a former Republican stronghold that Obama won in 2008. Will the people who voted so decisively against same-sex marriage be motivated to vote against him in November?

Some will, undoubtedly. But it was interesting that Obama’s all-but-certain GOP opponent, Mitt Romney, reacted to the president’s shift on gay marriage with a relatively subdued statement, reiterating his opposition but acknowledging that the issue is a “tender and sensitive topic.” The risk for Romney is that, although his position — he wants a federal constitutional amendment banning same-sex marriage — is popular among Republican primary voters, it might be seen as mean-spirited and punitive by the independents who will ultimately decide the election.

Politically, Obama may have taken a big step toward reclaiming the future.

The magic of hope and change that suffused his 2008 campaign has dissipated after 40 grueling months in office. Obama’s supporters could point to his accomplishments and cite the reasons why Romney would be a poor replacement, but the optimism and excitement were missing.

Obama could have kept silent on gay marriage, and frustrated progressives would still vote for him. Instead, he spoke out when he didn’t have to and took a stance that might hurt him in key states — reminding us how he can surprise and inspire.

Did I just catch a whiff of that hopey-changey stuff in the air?