0
template<typename T>
struct Test{
   void memberfunc(){
   }
}
template<typename T>
int func(T value){
   std::cout<<value<<std::endl;
}
/// may be the  class template POI
////some code here
int main(){
   Test<int> v;
   func(12.0); 
}
/// may be the template function POI  
////some code here

when I used the command "clang++ -Xclang -ast-print -fsyntax-only main.cpp" and the printed result show the complier generated the code

template<>
struct Test<int>{
   void memberfunc(){
   }
}

template<>
int func<dobule>(double value){
   std::cout<<value<<std::endl;
}

after their primary template,These code looks like User defined explicite Specilization code So
Question 1:
what the code the complier generated looks like at the point of instantiation?

Question 2:
what the difference between the code at the POI and the code explicite Specialization

xmh0511
  • 7,010
  • 1
  • 9
  • 36
  • Where's an explicit specialisation??? Such one would look like: `template<> struct Test { /* possibly totally different interface... */ };` All you have is an *instantiation* (`Test v;`). – Aconcagua Aug 14 '19 at 08:06
  • @Aconcagua So the code template<> struct Test{/**/} is what the complier generate at POI?It looks like explicite specilization – xmh0511 Aug 14 '19 at 08:24
  • *Explicit* means you write it on your own. Did you do so? No. So at best, you could say that instantiation is *implicit* specialisation. Explicit specialisations allow you to have totally different implementation: `template<> struct Test { int someTotallyDifferentFunction(std::string const&, double) const { /*...*/ } };` – note that the original `memberfunc` disappeared (in code, but the specialised template won't have it either!). – Aconcagua Aug 14 '19 at 08:52
  • Your questions are very unclear. The generated code "looks like" the code you're looking at (except it's most likely a syntax tree, not source code), and generated instantiations can be expected to "look like" specialisations, all with syntactically identical definitions. – molbdnilo Aug 14 '19 at 09:02
  • @Aconcagua Yes,I see the explicite specialization means what,I just want to understand,The form of the code at POI is equivalent the form of explicite specialization,for example,when Complier see the code Test v and immediatly generate the code looks like "template<> struct Test{/* the same content as the primary template */}" at the POI? – xmh0511 Aug 14 '19 at 09:05
  • Actually, that's pure internal representation. The compiler most likely won't generate any real C++ code at all, it rather will directly generate some kind of object code which is equivalent to the instantiated template, but that wouldn't differ from code generated from a separate `struct __Test__char { /* again same interface */ };`. – Aconcagua Aug 14 '19 at 09:15
  • @molbdnilo I'm sorry that I didn't ask the question clearly,I just want to know when the complier see code Test b and at the POI,the complier generated code Is it like I wrote these codes in that position looks like "template<> struct Test{ /* the content is same as the primary template */}"? – xmh0511 Aug 14 '19 at 09:16
  • @Aconcagua,Thanks ,So The complier may be generate the code like ````struct __Test__char { /* again same interface */ };```` The actally code only depend complier's internal implement,Instead of the form I wrote above? – xmh0511 Aug 14 '19 at 09:24
  • Such code, either as specialised template or as separate class most likely won't ever exist anywhere in memory. Compiling code is a pretty complex process, starting with parsing the file into separate tokens and organising these appropriately. From these tokens, it will deduce the code structure in some kind of internal representation. Why should now a compiler generate C++ again, which it would need to re-parse and tokenise??? If at all, it instantiates a template directly in its own internal representation, whichever this might be... – Aconcagua Aug 14 '19 at 09:40
  • @Aconcagua ,why I ask this question ,because https://stackoverflow.com/questions/57492168/what-actally-reason-of-the-specialization-redefined – xmh0511 Aug 14 '19 at 09:44
  • This has a different reason, see there... – Aconcagua Aug 14 '19 at 09:48

0 Answers0