So your value will have a type that either holds an int
or a vector of values of the same type.
This can be achieved with std::variant
with a struct to allow for the recursive nature of the type (and with one more constructor to allow initializing it with your desired syntax)
template<typename T>
struct nested_list : std::variant<std::vector<nested_list<T>>, T> {
using std::variant<std::vector<nested_list<T>>, T>::variant;
nested_list(std::initializer_list<nested_list> ilist)
: std::variant<std::vector<nested_list<T>>, T>(
std::in_place_index<0>, ilist) {}
// You can also add some helper methods like "get_vector", "is_vector", etc.
};
Usage example:
template<typename T>
std::ostream& operator<<(std::ostream& os, const nested_list<T>& lst) {
if (auto* v = std::get_if<0>(&lst)) {
os << '{';
bool first = true;
for (const auto& child : *v) {
if (first) first = false;
else os << ", ";
os << child;
}
os << '}';
} else if (auto* e = std::get_if<1>(&lst)) {
os << *e;
} else {
os << "<valueless by exception>";
}
return os;
}
int main() {
nested_list<int> x = {1, {2, 3}, 4, {5, {6}}, 7};
std::cout << x << '\n';
}
std::list<std::any>
? – Proxyint
? – Wherewithal