lundi 26 août 2019

Why is no compile-time error when calling an ambiguous ctor?

#include <iostream>
#include <vector>

int main()
{
    auto v1 = std::vector<std::size_t>(std::size_t{8});
    std::cout << v1.size() << std::endl;

    auto v2 = std::vector<std::size_t>{std::size_t{8}};
    std::cout << v2.size() << std::endl;
}

The code outputs:

8
1

I know this is a well-known problem in C++ because of:

std::vector<std::size_t>(std::size_t{8}) calls

explicit vector(size_type count) while

std::vector<std::size_t>{std::size_t{8}} calls

vector(std::initializer_list<T> init, const Allocator& alloc = Allocator()).

To my surprise:

Why does the second call not trigger a compile-time error for overload ambiguity?

Aucun commentaire:

Enregistrer un commentaire