lundi 21 décembre 2020

Explicit constructor in array of objects

struct Row
{
  Row() { puts("default"); }
  Row(const Row &other) { puts("copy"); }
  Row(Row &&other) { puts("move"); }
  explicit Row(int) { puts("conv. c'tor"); }  
};

int main()
{

  Row rs;   
  Row r[3] = {1, 3, rs};

  Row r3[3]{1, 3, rs}; // no conversion exists 

}

I know explicit constructor is not a candidate for copy initialization. But, How come when direct initializing results in error?

And also How array of objects allocated in memory? Is it same like array of scalar types? If I turn off elide constructors flag and remove explicit keyword, I get conv, move, conv, move and copy in first and also the same in second? can someone explain that?

Aucun commentaire:

Enregistrer un commentaire