2

In the abc.h file I have a class:

class ABC
{
    public:

        template <typename... Args>
        ABC(Args... args);

    protected:

        template <typename Type, typename... Args>
        void processArguments(Type &argument, Args... args);

        template <typename Type>
        void processArguments(Type &argument);

        template <typename Type>
        void doSomethingWithTheArgument(Type &argument);
};

Then, I created abc.cpp with the following:

template <typename... Args>
ABC::ABC(Args... args)
{
    processArguments(args...);
}

template <typename Type, typename... Args>
void ABC::processArguments(Type &argument, Args... args)
{
    doSomethingWithTheArgument(argument);
    processArguments(args...);
}

template <typename Type>
void ABC::processArguments(Type &argument)
{
    doSomething(argument);
}

template <typename Type>
void ABC::doSomethingWithTheArgument(Type &argument)
{
    std::cout << argument << std::endl;
}

And just some test.cpp:

int main()
{
    ABC abc(1, 2, 3, "somehing", 2 + 5);
    return 0;
}

And then I wanted to build it with the following command line:

g++ --std=c++11 abc.cpp test.cpp

And then bang!

/tmp/ccflWDpf.o: In function `main':
test.cpp:(.text+0x3b): undefined reference to `ABC::ABC<int, int, int, char const*, int>(int, int, int, char const*, int)'
collect2: error: ld returned 1 exit status

And when I put whole code in a single xyz.cpp file the code compiles fine. Does it tells that implementation of variadic templates cannot be provided in different translation unit?

no one special
  • 1,608
  • 13
  • 32

0 Answers0