I recently started using tuples instead of plain class members because I find it convenient to work with them. So my code looks something like this:
class TestClass final {
public:
TestClass() = default;
~TestClass() = default;
public:
template<int M>
auto get()->decltype(std::get<M>(m_private_members)) const {
return std::get<M>(m_private_members);
}
enum PrivateIdx {
count,
use_stuff,
name
};
private:
std::tuple<int, bool, std::string> m_private_members{1, true, "bla"};
};
So this can be used now like:
std::cout << t.get<TestClass::name>()> << std::endl;
This work also fine - the only thing is, adding members can be quite error-prone. One can easily get the access enums wrong by mixing up the order or forgetting a member. I was thinking of a macro style thing like:
PUBLIC_MEMBERS(
MEMBER(int count),
MEMBER(std::string name)
);
This would expand to the tuple and enum code. Problem is, I don't think this can be solved by a macro because it's two different data structures it would have to expand to, right? Also, I must admit, I've never looked into complicated macros.
I was also thinking of a template for solving this, but I could also not come up with a viable solution because enums cannot be generated by a template.