【发布时间】:2014-03-22 17:54:14
【问题描述】:
我正在专门研究头文件中模板类的成员函数,如下所示:
#pragma once
#include <iostream>
template<class T>
struct Test
{
void Print() { }
};
template<>
void Test<int>::Print()
{
std::cout << "int" << std::endl;
}
将特化放在头文件中是否正确(没有内联),还是应该放在 cpp 文件中?如上所示,它编译得很好(使用 VS2012),但我很惊讶我没有收到多个定义链接器错误。
【问题讨论】:
-
如果我将此代码包含在两个不同的文件中并实际使用
Test<int>::Print(使用 g++4.8.1),我确实会收到链接器错误。 -
VS2013 中没有链接器错误。 (甚至没有警告或任何东西,即使两个定义不同。)
标签: c++ specialization class-template