mercredi 18 septembre 2019

What's the best way to check uniform data?

I need an idea for the following situation (see code for clarity): Having several instances of struct MyData, what is the most concise way to check the instances for uniformity and returning a uniform instance. How can I reduce code duplication while maintaining readability? Uniformity means having the same values.

Given this example:

struct MyData {
   int foo;
   int bar;
   // ...
}

void print(MyData d)
{
    std::cout << "MyData[" << d.foo << ", " << d.bar << "]" std::endl;
}

MyData data1 = {5, 6};
MyData data2 = {5, 6};
MyData data3 = {5, 42};

I'm looking for a good code style to get the desired output:

isUniform(data1, data2);
 >>> MyData[5, 6]

isUniform(data1, data2, data3);
 >>> "Not uniform"

Aucun commentaire:

Enregistrer un commentaire