考虑以下示例:
#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 时会发生什么,为什么在这种情况下无法进行聚合初始化?我是否隐含地定义了某种构造函数?