You seem to have a big misunderstanding about this line:
T = NewT;
In C++, the T
is a template parameter and therefore it is static during compilation. You can not "set" it as it is not a variable. Unlike some dynamic languages (Python, ...) in C++ types are not objects and can not be modified during runtime. You can instantiate the template with different types (different T
s), but each is like a separate class (called a template class instantiation or class template instantiation, both are equivalent) and it is independent of the other instances.
Since the rest of your design is based on the above false assumption, you run into a lot of problems. The reason that the above line is not showing up as the first error is that it is not even instantiated before other part of the code are compiled (technically: different stages of compilation). That said, you need to understand why the above is impossible in C++ and the rest will follow. You may want to look into other implementations like Boost.Any to get some ideas on how it could be done. (Warning: It's more complicated than your approach)