cpp_weekly icon indicating copy to clipboard operation
cpp_weekly copied to clipboard

When to use explicit on a multiple parameters constructor

Open bananartist opened this issue 3 years ago • 0 comments

This is a c++ weekly episode request.

I recently had an argument with a colleague about marking explicit or not a constructor taking multiple parameters (of different types).

I've read what it does, but I'm not sure if this should be a best practice or not, to mark it explicit by default.

Note that for example, none of std::vector's multiple parameters constructors are marked explicit. So which one is used if a do:

std::vector<int> whatIsThisVector = { std::size_t{10}, 42};

It will be the one taking an std::initializer_list<int>, the number (10) and not the one taking the desired size and value with which to initialize the vector, the number (3).

https://en.cppreference.com/w/cpp/container/vector/vector

Why is this? Why isn't (3) marked explicit? (Since c++11).

bananartist avatar Aug 01 '22 22:08 bananartist