1

I'm struggeling with variadic templates in C++: I want to call a variadic template function implemented in class A from class B which inherits from A:

a.hpp

class A{
protected:
    template<typename... Ts>
    void variadic(Ts... args); 
};

a.cpp

#include "a.hpp"
template<typename... Ts>
void A::variadic(Ts... args) {/*something*/}

b.hpp

#include "a.hpp"
class B : public A {
public:
     void func() {
         variadic(4, 3.4, "hello");
     }
};

When I call the method (e.g. B testobj;testobj.func();) I always get the following linker error:

$ clang++ -std=c++11 main.cpp a.cpp -o main
/tmp/main-2e3fd9.o: In function `B::func()':
main.cpp:(.text._ZN1B4funcEv[_ZN1B4funcEv]+0x29): undefined reference to    `void A::variadic<int, double, char const*>(int, double, char const*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I implement the variadic method directly in the header file I don't get any error. If I put func in class A it will also compile without error.

It seems to me that the variadic template isn't visible in class B. Is there any way to solve my issue?

nee
  • 11
  • 1

0 Answers0