HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Trigger custom implicit conversion on "copy"

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
conversiontriggerimplicitcustomcopy

Problem

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

std::vector initialScene(Settings::HRes * Settings::VRes);

std::vector 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.

Solution

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.

Context

StackExchange Code Review Q#45968, answer score: 4

Revisions (0)

No revisions yet.