Expanding class members following open/closed principles
12:35 11 Sep 2026

I'm working out a good way to write a "repository" class that uses the same operations across multiple data members, which in my case are vectors of various types. Over time, new data types are necessary, so new vectors are added to the class, which seems to violate the open/closed principle (if I understand it right).

I'd like to access the data by type, so the solution probably involves templates. After some research I found a reasonable idea using tuples of vectors. Alternatively, I was considering using a template method that "dynamically" creates new "data members", and wanted to get some feedback on what parts are crummy and what make sense. Here are the pair of implementations:

#include 
#include 
#include 

template
class tuple_repo
{
        std::tuple...> t;
public:

        template 
        std::vector& get_vec()
        {
                return std::get>(t);
        }
};

template  // templating here prevents repo instances from sharing the same 
               // "static" values
class static_repo
{
public:
        template 
        std::vector& get_vec()
        {
                static std::vector v;
                return v;
        }
};


int main()
{
        tuple_repo t1;
        t1.get_vec().push_back(11);
        std::cout << t1.get_vec()[0] << std::endl;  // 11

        static_repo<1> r1;
        static_repo<2> r2;
        r1.get_vec().push_back(1);
        r2.get_vec().push_back(2);

        std::cout << r1.get_vec()[0] << std::endl;  // 1
        std::cout << r2.get_vec()[0] << std::endl;  // 2

}

When not using a template on the "static_repo", two instances seemed to share the same static vector.

Now it seems like a user could create the repository of their dreams without needing to edit its source code. It seems the difference between these two approaches is that the tuple approach requires specification at compile-time and the static approach doesn't require specification at all.

Are there ways to make this even better, or problems I need to fix/avoid? Is there a performance penalty for using static variables?

best-practices c++ templates template-instantiation