Forcing std::vector overload instead of int overload on list with one element

后端 未结 2 1501
[愿得一人]
[愿得一人] 2021-01-19 09:28

Consider the code below:

#include 
#include 

void f(std::vector v) {std::cout << __PRETTY_FUNCTION__ <<         


        
2条回答
  •  萌比男神i
    2021-01-19 09:41

    Forcing std::vector overload

    int main()
    {
        f(std::vector{42}); // the vector overload is being picked up now
    }
    

    Why isn't the vector(initializer_list) constructor being picked up?

    Assume that another header declares a void f(std::set v).

    How would you like the compiler to react when faced with f({1}): construct a vector or construct a set?

提交回复
热议问题