lundi 30 novembre 2015

initializer_list

Considering the following code snippet...

void boo(std::initializer_list<unsigned> l)
{
}

template <class T>
void foo(std::initializer_list<T> l)
{
    //Even though T is convertable, initializer list is not...:-(
    boo(move(l));
}

int main()
{
    foo({1u,2u,3u}); //Compiles fine as expected
    foo({1,2,3}); //Fails to compile at line 9... - could not convert...
    return 0;
}

... I'm surprised that initializer_list is not convertible to initializer_list, event though int converts to unsigned.

I've been wondering in what way one can write foo to allow the conversion. Can one somehow unwrap the list having the wrong type and recreate a new list with the right type?

Aucun commentaire:

Enregistrer un commentaire