473,651 Members | 2,485 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 1823

"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
3209
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
2746
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
5072
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
2314
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
1363
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
3533
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
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8697
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...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
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
7297
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
6158
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
4144
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...
1
2699
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
1587
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.