dimanche 13 juin 2021

Code size concerns with variadic templates

I am creating an algorithm that deals with many different user defined types at once. Each of these types will be assumed to have a func1 and func2 that will be the interface for MyAlgorithm. I can do this using variadic templates:

template <typename... Args>
class MyAlgorithm{
    // interact with Args using func1 and func2
};

I can now instantiate MyAlgorithm like so MyAlgorithm<A, B, C, D, E, F, G>. This works, however, I feel as though for my application this may result in too many specializations of MyAlgorithm. In practice, MyAlgorithm may be instantiated first like MyAlgorithm<A, B, B , A, C, D>, then MyAlgorithm<A, A, C, D>, and then MyAlgorithm<C, F D>, and so on.

Unfortunately, the template is instantiated for every combination of Args.

I had an idea that may be able to help, but I haven't gotten it exactly right. Instead of MyAlgorithm taking template parameters, it will deal with a template Proxy class that is specialized to the A, B, Cs.

// no template 
class MyAlgorithm{
    // interact with Proxy using func1proxy and func2proxy
    std::vector<Proxy> Args;
};

template<typename T>
class Proxy{
    // define func1proxy and func2proxy using Arg's func1 and func2
    std::unique_ptr<T> Arg; // only member
}

Of course this doesn't work because each Proxy specialization is a different class and so MyAlgorithm still needs to be a template class (and std::vector<Proxy> Args can't exist).

Is there a design pattern for what I want to do? Is there a design pattern that reduces the number of class instantiations?

Aucun commentaire:

Enregistrer un commentaire