今日見つけた話ですが、c++のラムダ式は変数の初期化にも使えますね。
test.h
extern const std::string global_str;
test.cpp
const std::string global_str = [] {
std::string str;
str.append("abc");
return str;
}();
さらに驚くことに、静的変数を初期化する場合そのラムダ式内では所在クラスのプライベート関数をアクセスできます。
test.h
class test_class {
test_class() {};
public:
const static test_class single_obj;
};
test.cpp
const test_class test_class::single_obj = [] {
test_class obj;
return obj;
}();