Template Specialization: when are extra template params required?

  • Thread starter Siegfried Heintze
  • Start date
S

Siegfried Heintze

I'm running on g++ v2.95 and 3.2.

Why is it that the print functions work fine without any extra template
parameters, but the SizeOfCod2 function requires that the bold code be
uncommented?



Thanks,

Siegfried



template<typename T>void print (const T& xx){ std::cout << xx <<
std::endl; }

template<>void print (const std::string& xx){ std::cout << "string = " <<
xx << std::endl; }

template<>void print (const R2::Cod_st& xx) { std::cout << "r2 cod = " <<
std::endl; }

template<>void print (const R3::Cod_st& xx) { std::cout << "r3 cod = " <<
std::endl; }



namespace CodAddress{

template<typename COD> unsigned int SizeOfCod2(COD* = 0){ return
sizeof(COD); }

template<> unsigned int SizeOfCod2/*<R2::Cod_st>*/(R2::Cod_st* ){

return R2::Cod_st::pARAM::BLOCKSIZE +
CodAddress::LUNCopyChapterAddress/*<R2::Cod_st>*/() +
CodAddress::LUNCopyChapterSize<R2::Cod_st>();

}

};
 
V

Victor Bazarov

Siegfried said:
I'm running on g++ v2.95 and 3.2.

Why is it that the print functions work fine without any extra
template parameters,

They have _arguments_ from which the template argument is deduced.
but the SizeOfCod2 function requires that the
bold code be uncommented?

Which code is "bold"? I am using text-only newsreader. Did you use
HTML? Please don't. Just mark certain lines in your code with some
symbols in comments so they can be referred to.
template<typename T>void print (const T& xx){ std::cout << xx <<
std::endl; }

template<>void print (const std::string& xx){ std::cout << "string
= " << xx << std::endl; }

template<>void print (const R2::Cod_st& xx) { std::cout << "r2 cod
= " << std::endl; }

template<>void print (const R3::Cod_st& xx) { std::cout << "r3 cod
= " << std::endl; }



namespace CodAddress{

template<typename COD> unsigned int SizeOfCod2(COD* = 0){ return
sizeof(COD); }

template<> unsigned int SizeOfCod2/*<R2::Cod_st>*/(R2::Cod_st* ){

This comment can be easily removed because the template argument is
deduced from the function argument.
return R2::Cod_st::pARAM::BLOCKSIZE +
CodAddress::LUNCopyChapterAddress/*<R2::Cod_st>*/() +

This one 'R2::Cod_st' needs to be here because there is no argument
to deduce the template argument from.
CodAddress::LUNCopyChapterSize<R2::Cod_st>();

}

};


V
 
S

Siegfried Heintze

{
This comment can be easily removed because the template argument is
deduced from the function argument.

I specifically added the R2::Cod_st* argument so it could deduce the
template argument. However this did not help. In its present form, it does
not compile. If I remove the /* and */ it compiles. Why cannot it deduce the
template argument as I have typed it above? The template argument can be
deduced from my print functions. What is different?
Thanks,
Siegfried
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top