Is there anything horribly wrong with creating different representations of data behind the scenes when constructing a std::vector<T> from a std::vector<U>?

std::vector<Color> initialScene(Settings::HRes * Settings::VRes);

std::vector<Pixel> finalScene(std::begin(initialScene), std::end(initialScene));

With the Pixel(const Color&) constructor doing something along the lines of:

explicit Pixel(const Color& color) {
   // Basically swaps blue and red channels
   data[0] = color.blue;
   data[1] = color.green;
   data[2] = color.red;

Note: this is not exactly what the converting constructor does; it's more of an illustration.


No problem with that at all, since it's not the copy which is doing the conversion (which would be strange) but the constructor. Since the vector templates you've mentioned are clearly of different types, the operation you describe would not at all be unexpected. It's little different from using std::copy to read from a file into an STL container, which is a common idiom.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.