有条件地初始化 const 的首选方法

Vor*_*rac 2 initialization constants c++11

初始化 const 自动变量的首选方法是什么?我能想到几个。

if 陈述:

const std::string s;
if( condition ) { const_cast<std::string&>(s) = "first"; }
else { const_cast<std::string&>(s) = "second"; }
Run Code Online (Sandbox Code Playgroud)

?: 操作员:

const std::string s = ( condition ) ? "first" : second;
Run Code Online (Sandbox Code Playgroud)

立即调用函数表达式:

const std::string s = [ & ] () 
    { if( condition ) return "first" else return "second" } ();
Run Code Online (Sandbox Code Playgroud)