Re: Template class parameter

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Arnaud,


I have followed your sample and tried it in my Visual Studio 2005
environment.
Here is the assembly code, what points do you want to show to us? I am not a
assembly language expert (but has some basic background). It is appreciated
if you could point out which are the key assembly language statements do you
mean. :-)

int main()
{
004113C0 push ebp
004113C1 mov ebp,esp
004113C3 sub esp,0C0h
004113C9 push ebx
004113CA push esi
004113CB push edi
004113CC lea edi,[ebp-0C0h]
004113D2 mov ecx,30h
004113D7 mov eax,0CCCCCCCCh
004113DC rep stos dword ptr es:[edi]
cout<<Factorial<4>::Value<<endl;
004113DE mov esi,esp
004113E0 mov eax,dword ptr [__imp_std::endl (418298h)]
004113E5 push eax
004113E6 mov edi,esp
004113E8 push 18h
004113EA mov ecx,dword ptr [__imp_std::cout (418290h)]
004113F0 call dword ptr
[__imp_std::basic_ostream<char,std::char_traits<char> >::operator<<
(418294h)]
004113F6 cmp edi,esp
004113F8 call @ILT+325(__RTC_CheckEsp) (41114Ah)
004113FD mov ecx,eax
004113FF call dword ptr
[__imp_std::basic_ostream<char,std::char_traits<char> >::operator<<
(41829Ch)]
00411405 cmp esi,esp
00411407 call @ILT+325(__RTC_CheckEsp) (41114Ah)

return 0;
0041140C xor eax,eax


regards,
George

"adebaene@xxxxxxxxxxxxxxxx" wrote:

On Nov 16, 4:44 am, George <Geo...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Hello everyone,

Sometimes a template class is defined to have only parameter and no type
(e.g. class something) information. Like the below sample. My question is,
why define template class in this way (e.g. I can implement in an alternative
way to put the parameter into constructor)? If there is no type information
(e.g. class something), there will be no benefits and reasons to use the
template class. Any comments?


The canonical example is a factorial class:

template<int N> class Factorial
{
static const int Value = N * Factorial<N-1>::Value;
};

template<> class Factorial<0>
{
static const int Value=1;
};

int main()
{
cout<<Factorial<4>::Value<<endl;
}

Take a look at the generated assembly for the main function....

Arnaud


.



Relevant Pages

  • Re: Template class parameter
    ... int main ... Sometimes a template class is defined to have only parameter and no type ... If there is no type information ... The canonical example is a factorial class: ...
    (microsoft.public.vc.language)
  • Facing problem in creating object of derived class (whish is derived from template class) and pushin
    ... void display; ... int main ... Also I want to know Is derived class is also a Template class? ...
    (microsoft.public.vc.stl)
  • Re: Template class parameter
    ... why define template class in this way (e.g. ... int* begin; ... Try doing this with a parameter passed to constructor, ... allocation. ...
    (microsoft.public.vc.language)
  • Re: Why do i get runtime error ?
    ... Even if you did not need a prototype for the variadic function, a function without one has no type information about any of its arguments. ... Before C99 this was not needed for functions with a fixed number of arguments, since a function without declaration was assumed to return int. ... make the second argument to printf match the specifier: ...
    (comp.lang.c)
  • g++ loop unrolling performance
    ... I am using the boost::array template class trying to generalize my ... can be determined at compile time or upon entry to the loop. ... The gcc switch ... int main ...
    (comp.lang.cpp)