Is there any way to create a compile time list of all those compenents? They are known at compile time so I guess it is possible. In the ideal case I would like to achieve unrolled loop, equivalent of this:
int main(int argc, char** argv) {
std::cout << "first" << '\n';
std::cout << "second" << '\n';
std::cout << "third" << '\n';
return 0;
}
That's the code:
#include <algorithm>
#include <iostream>
#include <list>
template<size_t N>
struct fixed_string {
constexpr fixed_string(const char(&string)[N]) {
std::copy_n(string, N, data);
}
char data[N];
};
class base_component {
public:
static inline std::list<base_component*> all_components;
virtual const char* get_name() = 0;
};
template <fixed_string name>
class component : public base_component {
public:
component() {
all_components.push_back(this);
}
virtual const char* get_name() {
return name.data;
}
};
static component<"first"> first;
static component<"second"> second;
static component<"third"> third;
int main(int argc, char** argv) {
for (auto object : base_component::all_components) {
std::cout << object->get_name() << '\n';
}
return 0;
}