1

I am wrapping a library into my class design. I would like to call a template method with unsigned int non-type parameter provided in the library in my class constructor.

#include <iostream>
#include <bar.h>  // template header in here
class foo {
    foo(unsigned num) {
        BAR<num>();  // a template method BAR inside "bar.h"
    }
};

When I tries to play around with this it seems that the non-type parameter is a constexpr. So the above code will generate error indicating that there is const error inside the function call.

So I decide to make the foo class a class template and pass this unsigned non-type parameter in foo's template argument parameter.

#include <iostream>
#include <bar.h>  // template header in here
template <unsigned num>
class foo {
    foo() {
        BAR<num>();  // a template method BAR inside "bar.h"
    }
};

This seems to be working well. However, I would like to separate the header and source file into separate .hpp/.cpp files. According to this thread, if I want to place template implementation inside .cpp source file, I have to explicitly instantiate all possible template argument at the end of .cpp file. For non-type parameter like unsigned integer, does that mean I have to instantiate thousands of possible unsigned int number to make the template available to all unsigned number object? Thanks for the help.

Community
  • 1
  • 1
yc2986
  • 1,101
  • 3
  • 20
  • 23

1 Answers1

0

I wouldn't recommend separating the implementation of a template class into a source file. If I understand your situation correctly then I don't think it's possible to do that unless you instantiate all possible values of the template parameter, which is impossible for an unsigned.

grigor
  • 1,584
  • 1
  • 13
  • 25
  • Does that mean I have to expose the template implementation anyway if I would like to use non-type as template argument? – yc2986 Oct 13 '16 at 00:04
  • I still want to separate the implementation. Is there another way to pass constexpr as parameter to constructor in this case? – yc2986 Oct 13 '16 at 00:11
  • @yc2986 Perhaps look into making the class and/or its constructor(s) `constexpr`? I'm not sure if it would actually do what you want, but it's the only thing I can think of that might. – Justin Time - Reinstate Monica Oct 13 '16 at 16:09