Update:. The comments show that the original problem you are trying to solve is not fully explained in the question. I will leave the original answer, however, at the bottom of this answer.
You cannot have two Derived<A> with different T2 types for the var member. In addition, a user-defined variable cannot affect the type of a member variable . Variable values ββare set at runtime, types are defined in compiletime.
To save a user-defined type, you will either have to limit the variable to a set of known types, or use a single type containing a serialized version of the variable's contents. A set of well-known types is often used in the context of databases, where fields can have one of several predefined types (for example, String, Integer, Boolean, Double). The type of the member variable can then be Boost.Variant, limited to C ++ representations of this type. Another application, "user-defined types", is where the user of your program must somehow determine the layout and interpretation of the type and its object, for example, if your program is an interpreter of some scripting language. In this case, Boost.Variant (or something similar) may be useful again, or, since the value is probably some user input, just store the serialized value in a string and interpret it every time you have to deal with it .
Original answer:
Usually this is done using metaprogramming of templates, in this case a function of the type (sometimes, depending on the context, part of the attributes, or policy class):
template <class T> struct DerivedMemVarType { typedef double type;
And then:
template <class T> class Derived : public Base { typedef typename DerivedMemVarType<T>::type T2; private: T2 var; };
You can also leave the default value, so any creation of Derived for a type that you did not display in your function will give a compilation error:
template <class T> struct DerivedMemVarType; //default is not defined template<> struct DerivedMemVarType<A> { typedef int type; }; template<> struct DerivedMemVarType<B> { typedef double type; }; //... Derived<C> dc; // ...template error mess.... // --> error: invalid use of incomplete type 'struct DerivedMemVarType<C>'