私は新しい自動生成された中括弧で囲まれた初期化子が好きです!
自分のコンストラクターを宣言し始めた場合にそれらを失うことを回避できる方法はありますか?
コード
#include <string>
struct Foo
{
int i;
std::string s;
// Foo() { } // I lose my brace-enclosed initializers if I uncomment this line
};
int
main( int argc, char* argv[] )
{
Foo f{ 1, "bar" }; // having the option to do this is good
return 0;
}
答え
以下のjuanchopanzaの回答に照らして、集計の長いルールを満たさなければならないようです。しかし、ボイラープレートコードを大量に必要としない50以上のORMクラス(ほとんどがそれぞれ5〜15フィールド)に適用できるソリューションが必要です。ボイラープレートコードがある場合は、少なくとも編集/保守が簡単です。
私が得ることができた最も近いものは、構成を使用したこのソリューションでした。もっと良い/もっとシンプルなものがあるのだろうか...
#include <string>
// this class must satisfy the rules for aggregates
struct Foo_
{
int i;
std::string s;
};
// this class can be anything...
struct Foo
{
Foo_ m_f;
Foo( Foo_ const& f ) : m_f( f ) { }
};
int
main( int argc, char* argv[] )
{
Foo f( { 1, "bar" } ); // ugly, but works
return 0;
}