mercredi 8 juillet 2020

Merge two constant `std::set`s in declaration (not in run-time)

I am trying to elegantly declare a constant std::set object that would be a merger of two other constant std::set objects.

#include <set>

const std::set<int> set_one = { 1,2,3 };
const std::set<int> set_two = { 11,15 };
const std::set<int> set_all = { 1,2,3,11,15 }; // this is not very elegant, duplication

Declaring set_all object this way is not too elegant, as it duplicates information from the previous two lines. Is there a way to use set_one and set_two constants in declaring set_all?

Something like this:

const std::set<int> set_all = set_one + set_two; // this does not compile, of course!
  1. All objects are strictly constants.
  2. There are no overlapping values in both source sets, so uniqueness will not be an issue.
  3. I know how to merge sets in runtime, this is not what I am looking for.
  4. I am really trying to avoid resorting to macros like this:
#include <set>

#define SET_ONE 1, 2, 3
#define SET_TWO 11, 15

const std::set<int> set_one = { SET_ONE };
const std::set<int> set_two = { SET_TWO };
const std::set<int> set_all = { SET_ONE, SET_TWO };

Aucun commentaire:

Enregistrer un commentaire