0

I have the following code structure for a template class:

// LagrangeMultiplier.hpp
template <int order>
class LagrangeMultiplier
{
...
};
// LagrangeMultiplier.cpp
#include "LagrangeMultiplier.hpp"

template <int order>
LagrangeMultiplier<order>::LagrangeMultiplier(...)
{
...
}

#include "LagrangeMultiplier.inst"
// LagrangeMultiplier.inst
template class LagrangeMultiplier<6>;
...
template class LagrangeMultiplier<5810>;

That is, I explicitly list the specific template instantiations in a file with a .inst extension and then include that at the end of my .cpp file. This is inspired by dealii's handling of templates.

This compiles fine and gives me what I want. However, whenever I try to use this class in another source file, Eclipse tells me that Type 'LagrangeMultiplier<order>' could not be resolved. This is annoying to me -- is there any way that I can get Eclipse to recognize these specific instantiations of the template class?

Edit: The following is how I would use it in another file:

// lagrange_test.cpp
#include "LagrangeMultiplier.hpp"

int main()
{
    constexpr int order = 2702;
    LagrangeMultiplier<order> lm();
    ...
    return 0;
}
Lucas Myers
  • 366
  • 2
  • 13
  • The other source file won't see those declarations of explicit instantiations of `LagrangeMultiplier`, assuming you're including `LagrangeMultiplier.hpp` and not `LagrangeMultiplier.cpp`. It seems like `#include "LagrangeMultiplier.inst"` should actually be in the header file, not the source file. – Nathan Pierson Jul 14 '21 at 16:40
  • @NathanPierson considering [this answer](https://stackoverflow.com/a/8752879/7736506) it seems to me that you need to put the specific template instantiations at the end of the `.cpp` file. Are you sure that it would work to put the specific instantiations at the end of the `.hpp` file? – Lucas Myers Jul 14 '21 at 16:45
  • I seem to have misunderstood how explicit template instantiation works. Not sure what's going on, then. – Nathan Pierson Jul 14 '21 at 16:47

0 Answers0