Search code examples
c++templatesmetaprogramming

Class templates with multiple unrelated arguments


I have a class template, that creates a class with two members:

template<typename coordinateType, typename ...DataTypes>
class Object{

    public:

        std::tuple<coordinateType, coordinateType, coordinateType> position;


        std::tuple<std::vector<DataTypes>...> plantData;

        

};

The issue is, rather than calling

auto myObject = Object<float, int, int, int>();

for an instance of Object with 3 ints of data, I want to clean this up and use two separate templates, without the unrelated "float" as the first argument.

Is there a way to implement this class so that it would be the equivalent of:

auto myObject = Object<float>();

myObject.track<int, int, int>();

And if not, is it possible to separate those two template arguments in any way, or am I stuck with grouping them together?


Solution

  • if you change the caller side a little, you can make track to return a new class.

    template<typename T, typename...Us>
    struct Object{
        std::tuple<T, T, T> position;
        std::tuple<std::vector<Us>...> plantData;
    };
    
    // you can also give it a different name, here I use a specialization instead
    template<typename T>
    struct Object<T>{
        template<typename...Us>
        Object<T,Us...> track();
    };
    
    void foo(){
        auto myObjectCreator = Object<float>();
        auto myObject = myObjectCreator.track<int, int, int>();
    }
    

    https://godbolt.org/z/qhPrEaa96