在abc.h文件中,我有一个类:
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);
};
然后,我用以下代码创建了abc.cpp:
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;
}
还有一些test.cpp:
int main()
{
ABC abc(1, 2, 3, "somehing", 2 + 5);
return 0;
}
然后我想用以下命令行构建它:
g++ --std=c++11 abc.cpp test.cpp
然后爆炸!
/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
当我将整个代码放在单个xyz.cpp文件中时,代码可以正常编译。它是否表明可变参数模板的实现不能在不同的翻译单元中提供?