c++ - Construct container with initializer list of iterators -
it's possible construct vector iterator range, this:
std::vector<std::string> vec(std::istream_iterator<std::string>{std::cin}, std::istream_iterator<std::string>{});
but can compile , run code using c++11 uniform initialization syntax (note bracers), this:
std::vector<std::string> vec{std::istream_iterator<std::string>{std::cin}, std::istream_iterator<std::string>{}};
what's going on here?
i know constructor taking initializer list gets priority on other forms of construction. shouldn't compiler resolve constructor taking initializer list containing 2 elements of std::istream_iterator
? should error std::istream_iterator
can't converted vectors value type std::string
, right?
from §13.3.2/1 ([over.match.list])
when objects of non-aggregate class type
t
list-initialized (8.5.4), overload resolution selects constructor in 2 phases:— initially, candidate functions initializer-list constructors (8.5.4) of class
t
, argument list consists of initializer list single argument.— if no viable initializer-list constructor found, overload resolution performed again, candidate functions constructors of class
t
, argument list consists of elements of initializer list.
in case initializer list constructor deemed non-viable (because std::istream_iterator<std::string>
not convertible std::string
), , second condition applies. results in constructor taking 2 iterators selected.
Comments
Post a Comment