4

Small example problem:

// foo.h
template <typename S>
struct Foo
{
    template <typename T>
    void Bar ();
};

Both S and T can be either char or int. 'Seems like an easy list to maintain, but here's the code:

// foo.cpp
template <typename S>
template <typename T>
void Foo<S>::
    Bar ()
    {
        // uses S and T
    }

template class Foo<int>;
template class Foo<char>;
template void Foo<int>::Bar<int>();
template void Foo<int>::Bar<char>();
template void Foo<char>::Bar<int>();
template void Foo<char>::Bar<char>();

And just to make it a complete example:

// main.cpp
#include "foo.h"

int main ()
{
    // uses all the Foo/Bar permutations
}

In my real-world example I have 2 outer types and 5 inner types for 2 methods, resulting in 2 + 3 * 2 * 5 = 32 ETIs!

I understand that when using ETIs I need to maintain a list of used arguments, but do I really need to maintain all the permutations?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Elliott
  • 2,603
  • 2
  • 18
  • 35
  • 2
    With variadic template you might **implicit** instantiate them... Why do you want all those explicit instantiations? – Jarod42 Feb 01 '21 at 09:11
  • @Jarod42, can we use implicit instantiation safely? ie. If I make a templated function that basically does the permutation calls, can I be sure that it won't be optimised out before the instantiation? eg. a const method where the return value is being ignored? I guess that this is the question I should've asked... – Elliott Feb 01 '21 at 09:21
  • @Elliott The calls might be optimized away but not the instantiated templates. – idmean Feb 01 '21 at 09:28
  • Implicit instantiations doesn't have to be part of `.obj`. So it is not reliable and might depends of compiler, but there are instantiated to check validity for example, that's also why I asked purpose of wanted ETIs. – Jarod42 Feb 01 '21 at 09:30
  • @Jarod42, thanks for your help. I use ETIs to reduce compile time of other translation units (I wasn't aware of another reason). I'm a little confused on your last comment. If you were to answer with a safe way to use implicit instantiation (at least for the above example), then I'd certainly accept that. – Elliott Feb 01 '21 at 09:39
  • @idmean, thanks. That would be ideal. I'd accept that as an answer with a little explanation as to why we can be use that the instantiation is reliable. Cheers. – Elliott Feb 01 '21 at 09:42
  • @Jaord42 is right, of course. I didn't think this through and I don't think there is a way without using macros, sorry. Please see the linked question above. – idmean Feb 01 '21 at 09:57

0 Answers0