mercredi 29 juin 2016

What are the rules of field-by-field constructor generation?

I have found that the possibility of usage of initializer list syntax for a class depends on whether or not the class fields have default value. Why?

To be precise, consider the following code:

class S
{
public:
  int a;
};
...
int a;
S s{ a };

It compiles with no problems. But if I add default value to the class field, it stops building:

class S
{
public:
  int a = 0;
};
...
int a;
S s{ a };

Error   1   error C2440: 'initializing' : cannot convert from 'initializer-list' to 'S'

So, the questions are: why? what else influences such constructor generation?

Aucun commentaire:

Enregistrer un commentaire