Given this code:
#include <array>
#include <algorithm>
#include <initializer_list>
struct P {
int r = 1;
int g = 2;
int b = 3;
P() = default;
P(std::initializer_list<int> l) {}
P(int i){}
};
int main() {
std::array<P, 4> array{};
std::ranges::fill(array, P{}); // works
P p = {}; // also works
std::ranges::fill(array, {}); // why isn't P inferred?
}
The first line works, because I explicitly create a new P{}
each time.
The second line works, and I think it’s because an initializer list can be implicitly converted to P, even if explicit
was present on the second ctor. But I’m not sure if that’s the reason.
The third line doesn’t work, but the second indicates to me that it should. Why doesn’t it? Shouldn’t the type of array
as the first param to fill
give enough indication to the compiler that the fill
function is of type P, and be able to use the second ctor?