jeudi 1 octobre 2015

Confusions around explicit template instantiation

Well, I think I just get extremely confused by explicit template instantiation ~>_<~

  1. Could an explicit instantiation declaration exploit an implicit instantiation definition?
  2. What if both explicit and implicit instantiation definitions exist in a program? Will they ultimately collapse into a single one?
  3. Does an explicit instantiation declaration have any effect when placed after an implicit instantiation definition?

Also, see the following code:

#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?
}

It causes the link error

/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

with GCC 5.2, but builds fine with clang 3.6. Which one is correct according to the standard?

I hope there is an insightful way to understand explicit template instantiation so that answers to all the questions above can be logically deduced and explained.

Aucun commentaire:

Enregistrer un commentaire