Код: Выделить всё
#include
#include
#include
enum class Field { kX, kY };
std::string ToString(const Field f) {
switch (f) {
case Field::kX:
return "x";
case Field::kY:
return "y";
default:
return "?";
}
}
std::set FieldStrings(const bool has_x, const bool has_y) {
std::set field_strings;
if (has_x) {
field_strings.insert(ToString(Field::kX));
}
if (has_y) {
field_strings.insert(ToString(Field::kY));
}
return field_strings;
}
template struct S {
int x = 0; // Should be present if and only if `kX` in `Args`.
int y = 0; // Should be present if and only if `kY` in `Args`.
// Should return `ToString` called on all of the `Field`s in `Args`.
static const std::set Fields() {
static const std::set kFields;
return kFields;
}
};
template struct T {
// Returns the fields that are available in the struct.
static const std::set &Fields() {
static const std::set kFields;
return kFields;
}
};
template struct T {
int y = 0;
static const std::set &Fields() {
static const std::set kFields = FieldStrings(false, true);
return kFields;
}
};
template struct T {
int x = 0;
static const std::set &Fields() {
static const std::set kFields = FieldStrings(true, false);
return kFields;
}
};
template struct T {
int x = 0;
int y = 0;
static const std::set &Fields() {
static const std::set kFields = FieldStrings(true, true);
return kFields;
}
};
- Шаблон класса, который принимает пакет параметров enums
- Участники данных S :: x и S :: y
Участники данных существуют условно, основываясь на полях, которые мы передаем в пакет шаблона < /li>
Статическая функция зависит от поля, которые мы передаем в Template < /li>
< /li>
< /li>
Подробнее здесь: https://stackoverflow.com/questions/797 ... ta-members