Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Explicit template instantiation for a templated function parameter

I want to write the definition of a templated function in the .cpp file, instead of it being in the header.

Let’s take this simple example:

// func.h

template <class T>
void print_message(T func) {
    func();
}

// main.cpp

#include <iostream>
#include "func.h"

void say_hello() {
    std::cout << "hello" << std::endl;
}

int main(int argc, char* argv[]) {
    print_message(say_hello);
    return 0;
}

How do I template instantiate the print_message function explicitly in the .cpp file, along the lines of how it is described here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried the following code snippet but I get this error: explicit instantiation of 'print_message' does not refer to a function template, variable template, member function, member class, or static data member.

// func.h
template <class T>
void print_message(T func) {
    func();
}

// main.cpp

#include <iostream>
#include "func.h"

void say_hello() {
    std::cout << "hello" << std::endl;
}

template void print_message<say_hello>(say_hello func);

int main(int argc, char* argv[]) {
    print_message(say_hello);
    return 0;
}

>Solution :

The issue is not that you provide the definition in the source. You did place the definition in the header. Moreover there is only a single translation unit in your example. The error would be the same if you place all code in main.cpp.

The issue is that print_message has a type argument, but say_hello is not a type.

This compiles whithout error:

#include <iostream>

// func.h
template <class T>
void print_message(T func) {
    func();
}

// main.cpp
void say_hello() {
    std::cout << "hello" << std::endl;
}

template void print_message<decltype(&say_hello)>(decltype(&say_hello) func);

int main(int argc, char* argv[]) {
    print_message(&say_hello);
    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading