lundi 29 janvier 2018

C++ std::initializer_list constructor

In code like this:

#include <iostream> 
#include <initializer_list>
#include <string>

struct A 
{
  A() { std::cout << "2" << std::endl; }
  A(int a) { std::cout << "0" << std::endl; }
  A(std::initializer_list<std::string> s) { std::cout << "3" << std::endl; }
  A(std::initializer_list<int> l) { std::cout << "1" << std::endl; } 
};

int main() 
{ 
 A a1; 
} 

Why does it call std::initializer_list specification of constructor? It'll generate ambiguity compilation error if we define, for example, constructor with std::initializer_list. What are the rules of such construction and why it's so specific about std::initializer_list with number as template parameter?

Aucun commentaire:

Enregistrer un commentaire