c++ - 函数模板的显式特化导致链接器错误

标签 c++ templates template-specialization

函数.h:

#pragma once
#include <iostream>

template<class T> void TemplatedFunction(T* p) {}

template<> void TemplatedFunction<float>(float* p) {}

template<> void TemplatedFunction<char>(char* p) {}

函数.cpp:

#include "Functions.h"

void Test()
{
    TemplatedFunction<float>(NULL);
    TemplatedFunction<char>(NULL);
}

主要.cpp:

#include "Functions.h"
void Test();

int main()
{
    Test();
    return 0;
}

构建错误:

main.obj : error LNK2005: "void __cdecl TemplatedFunction<float>(float *)" (??$TemplatedFunction@M@@YAXPAM@Z) already defined in Functions.obj
main.obj : error LNK2005: "void __cdecl TemplatedFunction<char>(char *)" (??$TemplatedFunction@D@@YAXPAD@Z) already defined in Functions.obj

我知道有两种方法可以解决这个问题:

  1. 不要将 Functions.h 包含到多个 .cpp 文件中 - 如果 h 文件包含许多 .cpp 文件所需的一些附加定义,则不能应用于复杂的项目。

  2. 将所有模板化函数声明为static。但这意味着专用函数出现在所有包含 Functions.h 的 .cpp 文件中,即使它们没有被使用,这可能会导致代码重复。

所以,我看到专门的模板函数表现得像非模板函数。如果没有 static 声明,是否还有其他解决方案可以防止链接器错误?如果函数被声明为 static,现代 C++ 编译器是否会将它们从优化构建中移除(如果它们未被使用)?

编辑。 阅读前两个答案后:我不在这里问如何防止此类链接器错误。假设我无法将特化移动到 .cpp 文件并将其保留在带有 staticinline 的 .h 文件中,这是否会导致优化构建中的代码重复和膨胀,当这些功能被添加到包含 .h 文件的每个 .cpp 文件中,即使它们未被使用?

最佳答案

So, I see that specialized templated function behaves like non-templated function.

正确。

Is there any other solution to prevent linker error without static declaration?

是的,就像任何普通的非模板函数一样:

Either 将函数特化定义为inline

在 header 中声明(但不定义)特化:

template<> void TemplatedFunction<float>(float* p);

template<> void TemplatedFunction<char>(char* p);

并在 Functions.cpp 中定义它们

template<> void TemplatedFunction<float>(float* p) {}

template<> void TemplatedFunction<char>(char* p) {}

这些是比使用 static 更好的选择,因为将函数设为静态会有其他副作用,例如在每个翻译单元中为函数提供不同的地址,并使每个翻译单元中的局部静态变量不同.这是语义上的变化,而不仅仅是链接器错误的解决方案。

If functions are declared static, does modern C++ compiler remove them from optimized build, if they are not used?

是的。但是在任何使用它们的文件中,您都会得到重复的代码,从而使可执行文件比只有一个函数定义时更大。

Assuming that I cannot move specialization to .cpp file and leave it in .h file with static ...

我看不出有什么必要这样做的充分理由,但无论如何......

... or inline, does this cause code duplication and bloating in optimized build, when these functions are added to every .cpp file where .h file is included, even if they are not used?

不会,如果给定文件中没有使用它们,它们将不会影响从该文件编译的对象的大小。

您的问题具有误导性,因为它似乎与模板有关(标题非常清楚地说明了模板!)但是未使用的内联/静态函数是否会导致代码膨胀与函数是否为模板无关。

如果您的问题只是“未使用的内联函数和静态函数会影响对象大小吗?”对于普通函数和函数模板,答案是否定的。

关于c++ - 函数模板的显式特化导致链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25529893/

相关文章:

C++ 指针不起作用?

templates - g:render标签将body()呈现为纯文本

c++ - 对称模板的参数

c++ - 为什么模板参数中的 enable_if_t 提示重新定义?

c++ - C++ 中 std::list<T>::begin() 的返回类型是什么

c++ - 不为派生类型调用函数模板特化

c++ - 为泛型类型专门化函数组

c++ - boost-asio 如何知道服务器正在运行的端口?

c++ - 如何理解 "C++ allows sizeof(char*) != sizeof(int*)"?

c++ - 这些语句是否相同 if (x - y) {} 与 if (x !=y) {}