如果标头位于预编译标头 xcode 中,则找不到默认模板参数

Default template parameter not found if header is in precompiled header xcode

本文关键字:默认 找不到 参数 于预 编译 xcode 如果      更新时间:2023-10-16

>我在test.h标头中有这样的简单函数

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

测试中.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>();

然后它起作用了。

如果不是声明和定义,我只留下定义......它编译。

据我了解,如果标头包含模板,则无法对其进行预编译,因为编译器仅在发现使用具有其他来源的具体类型的函数时,才从模板创建函数。没有具体类型 - 没有功能。

似乎

如果我将默认参数放在定义中而不是声明中,它似乎可以工作

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