Search code examples
c++classtemplatesderived

Template over multiple dimensions cpp


I have a project with multiple specializations but I need some "BasicStuff" in each of the specializations. Problem: I need to use some templates because only the specialization knows how some objects look like.

For Example I have some class "BasicData", "BasicDataHolder" and "BasicDataManager" where BasicData will be a class with timestamps. BasicDataHolder would hold the BasicData like a vector but with some more functions. The BasicDataManager would do stuff with multiple Objects of the BasicDataHolder.

Now the problem:

In the each of the specializations I have some "SpecialData" which is derived from BasicData. And I have to use sometime a BasicDataManager1 and in another specialization a BasicDataManager2 which are both derived from the BasicDataManager.

Unfortunatly templates are like voodoo for me but I tried it as the following:

    // BasicData.hpp
    class BasicData {
        int foo;
    };

    // BasicDataHolder.hpp
    template< typename T>
    class BasicDataHolder {
        static_assert(std::is_base_of<BasicData, T>::value, "!");
        vector<T> data;
        // other stuff
    };

    // BasicDataManager.hpp
    template <class T>
    // I tried something like template <template T> but it tells me
    // error: expected '<' before 'T'
    class BasicDataManager {
        // This Line is a problem
        static_assert(std::is_base_of<BasicDataHolder, T>::value, "!");
        vector<T> holderVector;
        // stuff
    };

    // specialDataManager
    template <class T>
    class SpecialDataManager : public BasicDataManager<T> {
        T dostuff();
        // other stuff
    };

    //specialDataHolder
    template <class T>
    class SpecialDataHolder : public BasicDataHolder<T> {
        // do some stuff
    };

    // Specialization:
    //specialData.hpp
    template <typename T>
    class SpecialData : public T {
        static_assert(std::is_base_of<BasicData, T>::value, "!");
        double bar;
        //other stuff
    };

Now I have to initialize the SpecialDataManager in my project-specialization. I tried it like

    // main.cpp
    #include "specialData.hpp
    #include "specialDataManager.hpp
    #include "specialDataHolder.hpp
    // This line is my problem, I think
    SpecialDataManager<SpecialDataHolder<SpecialData> > myManager;

I get multiple lines of error like: "error: 'SpecialDataManager' does not name a type"

My real problem is a dimension more but I think if I can solve this, it should be no problem anymore. Can someone help? I am not sure where I made any mistake...


Solution

  • It seems you want something like:

    class BasicData {
        int foo;
        // other stuff
    };
    
    template <typename T>
    class BasicDataHolder {
        static_assert(std::is_base_of<BasicData, T>::value, "!");
        using data = vector<T>;
        data mData;
        // other stuff
    };
    
    template <class T>
    class BasicDataManager {
        using holderVector = vector<T>;
        holderVector mData;
        // stuff
    };
    
    template <class T>
    class SpecialDataManager : public BasicDataManager<T> {
        T dostuff();
        // other stuff
    };
    
    template <typename T>
    class SpecialDataHolder : public BasicDataHolder<T> {
        // do some stuff
    };
    
    template <template T>
    class SpecialData : public T {
        static_assert(std::is_base_of<BasicData, T>::value, "!");
    
        double bar;
        //other stuff
    };