Skip to main content
gamedev.net
🔒 Locked

function pointers to template functions.

Started by Krylloan Nov 20, 2004 at 3:38 AM 2 replies 900+ views
Original Post
Krylloan
Krylloan
I'm writing a benchmark for a template class, using function pointers to call different functions. I pass a function pointer to the tester function, which repeats the function over an array of data elements for a specified number of times. I want to pass a function pointer to a template function. Unfortunately, there is no generated object code for the templated version of the function. Is there any way I can request the compiler to explicitly create object code inside a c++ file for a template function with a specific set of template parameters? eg: blah.h

template <typename T>
void add(T *data) {
    *data = (*(data + 1)) + (*(data + 2));
}



blah.cpp

#include "blah.h"

// Line below does not work, of course.
void add<u64>(u64 *data);

void tests() {
    // The line below causes a link error because there is no generated object code for add<u64>.
    test(add<u64>, 3 * sizeof(u64), 100, 100);
}



aaron_ds
aaron_ds
Could you post the source of your test function?
SiCrane
SiCrane
You can add an explicit template instantiation to generate the necessary object code. In your case it would look like:
template void add(u64 *);

Which you would put at namespace level in a source file.
Krylloan
Krylloan
Thanks SiCrane, that's exactly what I was after.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.