0

Is it possible to pass the template-parameters to a function somewhere else defined? For instance I have the class

Barrier.pp

template<Location L, Knock K>
class Barrier
{
  //...
  void checkBarrier( ... )
  {
    BarrierBest_checkBarrier<L, K>( ... );
  }
  //...
}

Other.cpp

template<Location L, Knock K>
BarrierBest_checkBarrier( ... )
{
  //Use L and K to do call other function
}

As I have it right now the compiler throws a unresolved external symbol for all the possible combinations of the template parameters, that is, BarrierBest_checkBarrier<1,1>, BarrierBest_checkBarrier<1,0>, BarrierBest_checkBarrier<0,1>, BarrierBest_checkBarrier<0,0>

Is there a way to make this work?

BRabbit27
  • 6,333
  • 17
  • 90
  • 161
  • Templates that need visibility in multiple translation units (cpp files) must be defined in header files and included in both. – metal Aug 22 '13 at 15:07
  • Please provide code that produces the same error that your question is about. – Oswald Aug 22 '13 at 15:11

2 Answers2

2

If there are a limited number of combinations, you can explicitly instantiate your template, allowing you to keep your code separated between header and source files.

Put this at the bottom of your source file

template void BarrierBest_checkBarrier<0, 0>(...);
template void BarrierBest_checkBarrier<0, 1>(...);
template void BarrierBest_checkBarrier<1, 0>(...);
template void BarrierBest_checkBarrier<1, 1>(...);

If you want other files calling your function to pick their own parameters not in this list, you must put the whole templated code into a header file.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • In the source file, you mean the .hpp or the .cpp ? What if BarrierBest_checkBarrier is not part of any class? – BRabbit27 Aug 22 '13 at 15:35
  • Source file = .cpp Oops I didn't notice BarrierBest_checkBarrier is not part of the class. I will update my answer. – Neil Kirk Aug 22 '13 at 15:38
  • I've been trying to make it work but I got `No instance template "BarrierBest_checkBarrier* matches the specified type`. – BRabbit27 Aug 23 '13 at 07:10
  • I have 3 files **Barrier.hpp** where the class is defined with a method called `checkBarrier()` that calls a function declared in **Other.cuh** as `template BarrierBest_checkBarrier( ... );` and implemented in **Other.cu**. In the latter is where I'm doing the explicit instantiation but I got the error. – BRabbit27 Aug 23 '13 at 07:41
  • @BRabbit27 Difficult to debug without seeing the code. Maybe best to start a new question? Also what does "cu" file extension mean? – Neil Kirk Aug 23 '13 at 10:51
0

The problem may be that your templated functions are in cpp files, not in headers. The template parameters have to be known at the compile time. If you have separate compilation units, they are not.

Patrik Beck
  • 2,455
  • 1
  • 19
  • 24
  • I have defined the functions in a separate header file and included it in both files but still get the same messages. The function `BarrierBest_checkBarrier` is not part of any class is just a method that helps me wrap a call to other stuff (CUDA kernels). BTW, probably is a stupid question but better to ask, is there a problem if the header file has extension ".cuh" – BRabbit27 Aug 22 '13 at 15:23
  • There should be no problem with different header extension. The whole definitions are in the header, not just the declarations? – Patrik Beck Aug 22 '13 at 15:30
  • After reading many times the answers provided I do understand what could be the problem. I'll try it. – BRabbit27 Aug 22 '13 at 15:31
  • The answer from @Neil Kirk is a good solution to your problem if you insist on keeping the functions in separate cpp file (compilation unit). – Patrik Beck Aug 22 '13 at 15:41