0

I have a class containing a template method with a non-type template parameter. The code size got really big, so I tried avoiding the inlining by putting it into the .cpp file. But I can only manage to instantiate it explictly for each non-type parameter.

Is an implicit instantiation possible? What would it look like? In an other related question this link http://www.parashift.com/c++-faq-lite/templates.html is provided but I can't find a solution for implicit instantiation (if there is something like this)...

class Example
{
  public: 
    template<enumExample T_ENUM> void Foo(void);
};

I get linker errors for Foo (unresolved external symbol) when using it.

Mirco
  • 107
  • 2
  • 11
  • `code size got really big` means binary size ? – iammilind Aug 03 '11 at 09:53
  • "so I tried avoiding the inlining by putting it into the .cpp file" - presumably yes. – Assaf Levy Aug 03 '11 at 10:19
  • @iammilind, yes it means binary size. The method is not that trivial and I call it more than twice. – Mirco Aug 03 '11 at 10:29
  • You are probably more interested in *explicit* template instantiation (rather than *implicit*, *implicit* is when the compiler instantiates as needed by compiling the source, *explicit* is when you tell it to instantiate the template for only particular parameters and in a controlled way and most code does not need to see the source for the template). The next few questions would be: *why do you want/need it to be a template at all*? If you make it a non templated function there will be a single instantiation of it. – David Rodríguez - dribeas Aug 03 '11 at 11:07
  • @David Rodríguez - dribeas, Before setting/clearing a flag in an memory-mapped device I need to differentiate between some registers and contained bits. I want to provide a Setter/Getter to do this. Switching a function parameter to first find the register and than the right bit in this costs a lot of RAM, so I thought of using templated methods and traits and move everything to compile-time. If I understand correctly, implicit instantation is exactly what I am looking for. I don't want to explicitly add an instantion for every Flag but let the compiler see when one is necessary... – Mirco Aug 03 '11 at 11:37
  • @Mirco: everything is a compromise. The most you move to compile time, the *faster* it will go, but at the same time the more code that your application will have. If you really have problems with the size of the binary, then consider moving it to runtime. Depending on the operations you might not loose that much performance (maybe you need some bit shifting or similar, but that is fast) and you will get an immediate reduction in code size. Note that each instantiation of the template is a function that needs to be created and linked in the executable. – David Rodríguez - dribeas Aug 03 '11 at 14:43

3 Answers3

2

Your problem is that the template code needs to be visible at the point at which it is instantiated. See C++ FAQ 35.13

Which basically means you can't do what you are trying to. There is an export keyword that makes this possible, but it is very poorly supported and I believe has been removed from the standard in C++0x. See C++ FAQ 35.14 for more information.

Charles Keepax
  • 2,392
  • 1
  • 18
  • 19
  • It has in fact been removed from the upcoming standard. The only vendor that implemented it in the current standard pushed for removal, so there was a good ground for agreement there. – David Rodríguez - dribeas Aug 03 '11 at 11:08
1

For implicit instantiation, the compiler needs to see the implementation of the function template. Usually this means that the implementation needs to be in a header file. If you just want to avoid inlining, you could try writing the implementation of the function template in the header, but outside of the class declaration (although I'm not sure inlining is your real problem).

imre
  • 1,667
  • 1
  • 14
  • 28
0

To reduce code size you can try to reduce the dependencies by implementing, when appropriate, the pimpl idiom.

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87