好吧,我想我对显式模板实例化感到非常困惑~>_<~
- 显式实例化声明能否利用隐式实例化定义?
- 如果程序中同时存在显式和隐式实例化定义怎么办?他们最终会崩溃成一个单一的吗?
- 显式实例化声明放在隐式实例化定义之后是否有任何影响?
另外,请参见以下代码:
#include <iostream>
#include <vector>
std::vector<int> a; // Implicit instantiation definition.
// Explicit instantiation declaration.
extern template class std::vector<int>;
int main() {
std::cout << std::vector<int>().size(); // So what?
}
导致链接错误
/tmp/ccQld7ol.o: In function `_GLOBAL__sub_I_a':
main.cpp:(.text.startup+0x6e): undefined reference to `std::vector<int, std::allocator<int> >::~vector()'
collect2: error: ld returned 1 exit status
使用 GCC 5.2,但使用 clang 3.6 构建良好。根据标准,哪一个是正确的?
我希望有一种有见地的方法来理解显式模板实例化,以便可以从逻辑上推断和解释上述所有问题的答案。