vsoftco vsoftco - 1 year ago 136
C++ Question

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

Consider the code below:

#include <iostream>
#include <vector>

void f(std::vector<int> v) {std::cout << __PRETTY_FUNCTION__ << std::endl;}
void f(int n) {std::cout << __PRETTY_FUNCTION__ << std::endl;}

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

Live on Coliru

I was a bit surprised to realize that in this case the int overload is being picked up, i.e. the output of the program is:

void f(int)

with the warning

warning: braces around scalar initializer [-Wbraced-scalar-init] f({42});

Of course this happens only when I pass a 1-element list as an argument, otherwise the
overload is being picked up.

Why is
treated like a scalar and not like a init-list? Is there any way of forcing the compiler to pick the
overload (without explicitly constructing
) even on 1-element lists?

PS: The
has an init-list constructor

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

see (7) from cppreference.

Answer Source

Braced initializer has no type, we can't say {42} is an int or std::initializer_list<int>. When it's used as an argument, special rules for overload resolution will be applied for overloaded function call.

(emphasis mine)

  • Otherwise, if the parameter type is not a class and the initializer list has one element, the implicit conversion sequence is the one required to convert the element to the parameter type

{42} has only one element with type int, then it's exact match for the overload void f(int). While for void f(std::vector<int>) a user-defined conversion is needed. So void f(int) will be picked up here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download