简体   繁体   English

将可变参数模板参数包传递给下一个函数

[英]Passing variadic template argument pack to next function

I have done my own reimplementation of printf() (classic) in my debugging code. 我在调试代码中完成了自己的printf()(经典)重新实现。

    template<typename T, typename ...Args>
    void Printf(wchar_t const * message, T value, Args ...args);
    void Printf(wchar_t const * message);
    void Printf();

It uses variadic templates and works perfectly fine. 它使用可变参数模板,工作得很好。

Now I want to use Printf() in couple of functions that are going to accept almost same arguments but perform Printf() in different ways. 现在我想在几个函数中使用Printf(),它们将接受几乎相同的参数但以不同的方式执行Printf()。 Here is one of the client functions for Printf() 这是Printf()的客户端函数之一

template<typename ...Args>
void Debug::Line(unsigned int in_tabs, wchar_t const * in_string, Args...in_args){
    for (unsigned int i = 0; i < in_tabs; ++i)
        Tab();
    Printf(in_string, in_args...);
    NewLine();
}

As soon I start using this construct, I start getting UNRESOLVED linker errors 一旦我开始使用这个结构,我开始得到UNRESOLVED链接器错误

error LNK2019: unresolved external symbol "public: static void __cdecl Nerv::Debug::Line<>(unsigned int,wchar_t *)" (??$Line@$$$V@Debug@Nerv@@SAXIPA_W@Z) referenced in function "public: void __thiscall Nerv::UI::NervUIRect::DebugRect(void)" (?DebugRect@NervUIRect@UI@Nerv@@QAEXXZ)
error LNK2019: unresolved external symbol "public: static void __cdecl Nerv::Debug::Line<int,int>(unsigned int,wchar_t *,int,int)" (??$Line@HH@Debug@Nerv@@SAXIPA_WHH@Z) referenced in function "public: void __thiscall Nerv::UI::NervUIRect::DebugRect(void)" (?DebugRect@NervUIRect@UI@Nerv@@QAEXXZ)

It's strange to me, cause I've tested this construct on a simpler case first and it worked fine. 这对我来说很奇怪,因为我首先在一个更简单的情况下测试了这个结构并且它工​​作正常。

template<typename T,typename...Ts>
void Printf(T value,Ts...args){
    OutputDebugString(value);
    Printf(args...);
}
void Printf(){

}

template<typename...Args>
void UsePrintf(Args...args){
    Printf(args...);
}

The difference from the simple case is that all theese functions (that don't work) are static members of a Debug class and that there are some additional function arguments. 与简单情况的不同之处在于,所有的theese函数(不起作用)都是Debug类的静态成员,并且还有一些额外的函数参数。 So how am I supposed to deal with this? 那我该怎么处理呢?

正如TartanLlama指出的那样,模板函数体需要在头文件中可用/通过将定义和声明置于头部中或者通过在头部末尾包含带有函数实现的cpp文件来实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM