I use the mixin pattern a lot in my C++ programming. I love this pattern as it has many of the advantages of the pipeline pattern but higher performance (I hope (let me know if anyone knows the internals of the optimizer to be sure)).
By “mixin” a mean a template class that inherits from one of its template arguments:
template<class BASE_CLASS>
class My_Mixin : public BASE_CLASS
…
I have been using this pattern for years without issue. Sometimes I have nested them 30 layers deep. It is nice because you can remove things for testing and build things up slowly etc, and not have to worry that performance will suck if you put it in a DSP or rendering loop.
Recently, I have had need to use, base classes which are templated container classes not other mixins and it has been desirable for the mixins to know the type that the base class is templatized on. I tried a bunch of things many of which did not compile as expected. This was the best I could do but it is kind of ugly.
I am running 14 until android studio NDK lets me move to something newer though I am still trying to master 11 completely. At the moment though I am developing on MS Visual Studio targeting windows 10 but with the goal that much of my code will be cross platform.
Often using auto has solved most problems but not in this case. So I have added this dummy static variable.
Does anyone know if there is a better way to do this?
template<class DATA>
class Base
{
public:
static DATA data_example;
private :
std::vector<DATA> x; // something I want to store but My_Mixin can’t get the type
// from with a decltype
};
template<class BASE_CLASS>
class My_Mixin: public BASE_CLASS
{
public:
private:
typedef decltype(BASE_CLASS::data_example) Data_Type;
Data_Type my_mixins_data; // I need to store something of the type that the base class was instantiated on
};
int main()
{
My_Mixin<Base<int>> obj;
}
I think I have dealt with this in the past like this
template<class BASE_CLASS, class DATA >
class My_Mixin: public BASE_CLASS
and then in main
My_Mixin<Base<int>, int> obj;
But the real code I am working on has about 6 mixins and 3 templated arguments for the base class.
Thanks for your ideas.
template<class DATA>
class Base
{
public:
using data_example = DATA; // Note
private :
std::vector<DATA> x; // something I want to store but My_Mixin can’t get the type
// from with a decltype
};
template<class BASE_CLASS>
class My_Mixin: public BASE_CLASS
{
public:
private:
// Note:
typename BASE_CLASS::data_example my_mixins_data; // I need to store something of the type that the base class was instantiated on
};
int main()
{
My_Mixin<Base<int>> obj;
}
… a template class that inherits from one of its template arguments … Recently, I have had need to use, base classes which are templated container classes