次のコードがあります。基本的に、集計初期化構文を使用して、POD 以外の構造体の std::array を初期化したいと考えています。g++ 4.6 と 4.7 (最新の週次スナップショット) の両方がコードのコンパイルに失敗します。
#include <array>
struct TheClass {
int a1, a2;
TheClass(int b1, int b2) : a1{b1}, a2{b2} {};
};
template<unsigned D>
struct OtherClass {
std::array<TheClass, D> a;
OtherClass(std::array<TheClass, 2> b) : a{b} {};
};
int main()
{
OtherClass<2>{{ {1, 2}, {2, 3} }}; //tried a lot of options here
}
GCC エラー:
v.cpp: In function ‘int main()’:
v.cpp:20:37: error: no matching function for call to ‘OtherClass<2u>::OtherClass(<brace-enclosed initializer list>)’
v.cpp:20:37: note: candidates are:
v.cpp:15:5: note: OtherClass<D>::OtherClass(std::array<TheClass, 2ul>) [with unsigned int D = 2u]
v.cpp:15:5: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::array<TheClass, 2ul>’
v.cpp:11:8: note: constexpr OtherClass<2u>::OtherClass(const OtherClass<2u>&)
v.cpp:11:8: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const OtherClass<2u>&’
v.cpp:11:8: note: constexpr OtherClass<2u>::OtherClass(OtherClass<2u>&&)
v.cpp:11:8: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘OtherClass<2u>&&’
私の質問: 上記のコードは正しいですか? std::array は集約であるため、そのデータ メンバーを構築するのに問題はないようです。多分それはGCCのバグですか?
編集:
OtherClass<2>{{ TheClass{1, 2}, TheClass{2, 3} }};
もちろん機能しますが、多くの場所でクラスを構築する必要があるため、それを使用したくありません。C++11 は の省略をサポートする必要がありTheClass
ます。この質問も参照してください。