dimanche 5 mars 2017

How does the compiler decide between vector vs initializer_list?

In the code below, with both versions of the print method present the first call resolves to the one with initializer_list. If I comment out the definition with initializer_list, the program seamlessly uses the vector version. In the first case I was expecting the compiler to complain!

#include <iostream>
#include <vector>
using namespace std;

void print(const vector<int>& v1){
        cout << "vector \n";
}
void print(const initializer_list<int>& il) {
         cout << "init list \n";
}

int main() {
        print({1,2,3,4,5});
        return 0;
}

Aucun commentaire:

Enregistrer un commentaire