I was playing around with std::vector
initialization when I was kind of surprised to discover that I can "transparently" initialize a std::vector
of double
s from a std::vector
of float
s. This also works with std::vector::insert
.
#include <vector>
// fmt library
#define FMT_HEADER_ONLY
#include <fmt/core.h>
#include <fmt/ranges.h>
int main() {
std::vector<float> f_v = { 1, 2, 3 };
std::vector<double> d_v = std::vector<double>(std::begin(f_v), std::end(f_v));
d_v.insert(std::end(d_v), std::begin(f_v), std::end(f_v));
fmt::print("{}\n", d_v);
float f_a[] = {1, 2, 3};
std::vector<double> d_a = std::vector<double>(f_a, f_a + 3);
d_a.insert(std::end(d_a), f_a, f_a + 3);
fmt::print("{}\n", d_a);
}
Am I overlooking something or is it really that easy?