次の例を検討してください。
#include <iostream>
#include <string>
struct ABC
{
std::string str;
unsigned int id ;/* = 0 : error: no matching constructor for initialization of 'ABC'*/
};
int main()
{
ABC abc{"hi", 0};
std::cout << abc.str << " " << abc.id << std::endl;
return 0;
}
id clang 3.x および gcc 4.8.x のデフォルト値なしで構造体 ABC を定義すると、コードは問題なくコンパイルされます。ただし、「id」のデフォルト引数を追加すると、次のようなエラー メッセージが表示されます。
13 : error: no matching constructor for initialization of 'ABC'
ABC abc{"hi", 0};
^ ~~~~~~~~~
4 : note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
struct ABC
^
4 : note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
4 : note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 2 were provided
1 error generated.
Compilation failed
技術的な観点から、デフォルトの引数で id を定義するとどうなりますか?また、その場合、集計の初期化ができないのはなぜですか? ある種のコンストラクターを暗黙的に定義しますか?