September 2017
Beginner to intermediate
384 pages
9h 18m
English
You might have noticed, way back when we presented the implementation of std::copy, that the value_type of the two iterator type parameters were not constrained to be the same. This is a feature, not a bug! It means that we can write code that relies on implicit conversions and it will just Do The Right Thing:
std::vector<const char *> input = {"hello", "world"}; std::vector<std::string> output(2); std::copy(input.begin(), input.end(), output.begin()); assert(output[0] == "hello"); assert(output[1] == "world");
Looks trivial, right? Look closely! Deep within our instantiation of std::copy is a call to the implicit constructor that converts const char * (the type of *input.begin()) to std::string (the ...
Read now
Unlock full access