我在test.h标头中有一个简单的功能

template <class Dst = std::string, class T>
Dst format(const T&);

template <class Dst, Class T>
Dst format(const T&)
{
 return Dst();
}


在test.cpp中

#include "stdafx.h"
#include "test.h"
#include <iostream>
int main(int argc , char** argv)
{
    std::string f = format("");
    std::cout << f;
    return 0;
}


如果将此标头添加到xcode中的预编译标头中

该代码不再编译。

我收到“没有匹配的函数调用”错误。

如果我手动将默认参数添加到函数调用

format<std::string>();


然后就可以了。

如果没有声明和定义,我只保留定义...进行编译。

最佳答案

据我了解,如果头文件包含模板,则无法对其进行预编译,因为编译器仅在找到将这些功能与其他来源的具体类型一起使用时才通过模板进行功能。没有具体类型-没有功能。

关于c++ - 如果 header 在预编译的 header xcode中,则找不到默认模板参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41765470/

10-10 21:24
查看更多