473,802 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template Specialization: when are extra template params required?

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<typena me 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<typena me COD> unsigned int SizeOfCod2(COD* = 0){ return
sizeof(COD); }

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

return R2::Cod_st::PAR AM::BLOCKSIZE +
CodAddress::LUN CopyChapterAddr ess/*<R2::Cod_st>*/() +
CodAddress::LUN CopyChapterSize <R2::Cod_st>( );

}

};


Feb 1 '06 #1
2 1413
Siegfried Heintze wrote:
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<typena me 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<typena me 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::PAR AM::BLOCKSIZE +
CodAddress::LUN CopyChapterAddr ess/*<R2::Cod_st>*/() +
This one 'R2::Cod_st' needs to be here because there is no argument
to deduce the template argument from.
CodAddress::LUN CopyChapterSize <R2::Cod_st>( );

}

};

V
--
Please remove capital As from my address when replying by mail
Feb 2 '06 #2
{

template<typena me 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.


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
Feb 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
7691
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
2
7953
by: Xenos | last post by:
The new version of GCC is out and in its list of changes, it talks about the C++ Standard's requirements for using the typename and template keywords to disambiguate dependent names. I'm use to seeing typename being used, but I've never seen template used for this purpose. I've studied there example for a long time, and still don't understand what situations call for the template keyword (and reading the standard, to me, is Greek). Can...
4
370
by: nsgi_2004 | last post by:
When you make a specialized class, does it "inherit" everything from the general version as well. Essentially, I just need to add one function to a special instance. Here is a small version of the code: template <typename T, int n> class Vector { public: Vector<T, n> add(Vector<T, n>& rhs) {
13
2831
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; template <typename Base> class Tpl : protected Base {
3
2733
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename Foo> class Bar { private: Foo foo;
9
1593
by: Hendrik Schober | last post by:
Hi, this code #include <iostream> template< typename T > struct X { void f(); };
2
1853
by: Glenn G. Chappell | last post by:
I am trying to write two constructors for the same class. One takes an iterator and so is a template. The other takes a particular type by reference to const. class Foo { public: template<typename InputIterator> Foo(InputIterator i); Foo(const Bar & b);
5
2012
by: Gernot Frisch | last post by:
// - in my xy.cpp file -- template <const int Ttex, const int Tcol, const int Tlight, int TzBuffer> struct MyFragmentShader { static const int varying_count = Ttex*2 + Tcol*3 + Tlight; }; ....
8
2971
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.