473,729 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template<> confusion

Consider the following:

class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}

template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};

This code compiles on MS VS C++ 2003 and Intel C++ 9 but not on Dev-C++
4.9.9.2. However, if I comment out the line "template<> ", it compiles
fine on all 3 compilers.

Can someone please explain what is the standard C++ on this matter?
Thank you.

Chris

Feb 13 '06 #1
14 1839

"SoilMan" <cb****@yahoo.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Consider the following:

class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}

Move the following out of this scope: template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};


and place it here:

template <>
void xyz::foo(int x)
{
cout << "foo<int> " << x << endl;
}

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hc ltech.com>
Feb 13 '06 #2
dc
There is no need to place template <> when u defining a specific
implementation.

Feb 13 '06 #3

SoilMan wrote:
Consider the following:

class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}

template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};

This code compiles on MS VS C++ 2003 and Intel C++ 9 but not on Dev-C++
4.9.9.2. However, if I comment out the line "template<> ", it compiles
fine on all 3 compilers.

Can someone please explain what is the standard C++ on this matter?


A specialization of member template can only be declared out of the
class definition.

Anyway, you don't need specialization here, function overloading would
suffice.

Feb 13 '06 #4
dc wrote:
There is no need to place template <> when u defining a specific
implementation.


It depends on what you want. If you don't place the template<>, you get an
overload instead of a specialization.

Feb 13 '06 #5
Sumit Rajan wrote:
"SoilMan" <cb****@yahoo.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Consider the following:

class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}


Move the following out of this scope:
template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};


and place it here:

template <>
void xyz::foo(int x)
{
cout << "foo<int> " << x << endl;
}


Just out of curiosity, shouldn't it be:

template<>
void xyz::foo<int>(i nt x)
{
cout << "foo<int> " << x << endl;
}
Feb 13 '06 #6

"red floyd" <no*****@here.d ude> wrote in message
news:Rd******** **********@news svr11.news.prod igy.com...
Sumit Rajan wrote:

Move the following out of this scope:
template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};


and place it here:

template <>
void xyz::foo(int x)
{
cout << "foo<int> " << x << endl;
}


Just out of curiosity, shouldn't it be:

template<>
void xyz::foo<int>(i nt x)
{
cout << "foo<int> " << x << endl;
}


IIRC, it is not necessary.

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hc ltech.com>
Feb 14 '06 #7
dc
Can someone explain when u need overloaded version / specialization of
a template. And
how does it differs...

Feb 14 '06 #8

Rolf Magnus escreveu:
dc wrote:
There is no need to place template <> when u defining a specific
implementation.


It depends on what you want. If you don't place the template<>, you get an
overload instead of a specialization.


IIRC, in either case you get an overload, for there is no such thing as
function template specialization.

Please advise me if I got it wrong.

TIA,

Marcelo Pinto.

Feb 14 '06 #9
dc
Ya , for function it doesnot matter. If both implemented , overload
will be preferred over
specialization.
But for class template, only specialization is the solution.

Feb 14 '06 #10

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

Similar topics

2
3224
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are writing one object in C# which will take the entire table tag contents and renders. Ie., we need to pass "<table>………… <thead>……</thead>. <tr>.<td> <td>..<tr>.<td> <td> </table>" content to
4
2758
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... }; idiom (don't know the name of it, if there is one). The problem is that I don't want any of my classes to have public constructors. They should be created by a static member function. This is what I want to do, save for the fact that this...
4
5078
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template< template<class> class T> class A { }; int main() { return 0;
4
2316
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the example below //using Tparent::Tptr; //typedef Tparent::Tptr Tptr; Consider example:
3
1368
by: George2 | last post by:
Hello everyone, I sometimes saw code with template<and followed by a class definition, like, template<class { // class definition
10
3545
by: jason.cipriani | last post by:
Is there any difference between declaring a template parameter as a "typename" or a "class"? E.g. template <class TT f() { } template <typename TT g() { } Thanks, Jason
0
9436
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
9298
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8170
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...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6028
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
4799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3248
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2705
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2173
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.