Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error message "undefined reference to template function passed as template parameter"

Tags:

When I pass a template function as a template parameter of a base class, the linker complains that it cannot link the function:

#include <stdio.h>  template<int I> inline int identity() {return I;} //template<> inline int identity<10>() {return 20;}  template<int (*fn)()> class Base { public:     int f() {         return fn();     } };  template<int Val> class Derived : public Base<identity<10> > { public:     int f2() {         return f();     } };  int main(int argc, char **argv) {     Derived<10> o;     printf("result: %d\n", o.f2());     return 0; } 

Results in:

$ g++ -o test2 test2.cpp && ./test2 /tmp/ccahIuzY.o: In function `Base<&(int identity<10>())>::f()': test2.cpp:(.text._ZN4BaseIXadL_Z8identityILi10EEivEEE1fEv[_ZN4BaseIXadL_Z8identityILi10EEivEEE1fEv]+0xd): undefined reference to `int identity<10>()' collect2: error: ld returned 1 exit status 

If I comment out the specialization, then the code compiles and links as expected. Also, if I inherit from Base<identity<Val> > instead of Base<identity<10> >, the code works as I expect.

Try here: http://coliru.stacked-crooked.com/a/9fd1c3aae847aaf7

What do I miss?

like image 659
Ferenc Avatar asked Dec 30 '16 15:12

Ferenc


People also ask

Why am I getting “undefined” error when adding a template?

If you see the error message “Undefined” when you try to add a template: In some cases, the latest version of PHP may cause undefined errors. Although staying up to date with PHP versions is often a good idea, it may cause problems for edge cases.

What does error code invalidtemplate mean?

Code=InvalidTemplate; Message=Deployment template validation failed: 'The provided value {parameter value} for the template parameter {parameter name} is not valid. The parameter value is not part of the allowed values

Why did my template fail validation?

This error can result from several different types of errors. They usually involve a syntax or structural error in the template. If you receive an error message that indicates the template failed validation, you may have a syntax problem in your template. Code=InvalidTemplate Message=Deployment template validation failed

What does undefined reference to mean in C++?

7 April 2017 in C++ tagged C++ / template / undefined reference to by Tux In case you have a project where you use a templated class that is split in its own header (.h) and source (.cpp) files, if you compile the class, into an object file (.o), separately from the code that uses it, you will get the undefined reference to error at linking.


1 Answers

It seems the problem is a gcc error: the code compiles and links with clang, icc, and the EDG frontend. A potential work-around not changing any of the uses would be the use of a class template identity instead of a function:

template<int I> struct identity {     operator int() { return I; } };  template<typename fn> class Base { public:     int f() {         return fn();     } }; 
like image 121
Dietmar Kühl Avatar answered Oct 18 '22 18:10

Dietmar Kühl