473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Uniqueness of class template names

The following compiles and works with g++ 3.4.4 and Borland C++
Builder 6 update#4:

#include <iostream>
#include <vector>
#include <utility>

// declaration and definition for primary class template
template <class T>
class A
{
public:
void pr()
{
std::cout << "A<T>" << std::endl;
}
};

// declaration and definition for a class template specialization
template <class T>
class A<std::vector<T
{
public:
void pr()
{
std::cout << "A<std::vector<T" << std::endl;
}
};

// redeclaration (and definition) of the primary class template - this
time with two template parameters
template <class X, class Y>
class A<std::vector<std::pair<X,Y >
{
public:
void pr()
{
std::cout << "A<std::vector<std::pair<X,Y >" << std::endl;
}
};

int main(int , char* [])
{
A<int a;
A<std::vector<int b;
A<std::vector<std::pair<int,char c;

a.pr();
b.pr();
c.pr();
}

The output of the program is:
A<T>
A<std::vector<T
A<std::vector<std::pair<X,Y >

But, as stated in the C++ 1998 standard chapter 14, paragraph 5:
"A class template shall not have the same name as any other template,
class, function, object, enumeration, enumerator, namespace, or type
in the same scope (3.3), except as specified in (14.5.4)."

So, how is it possible to first declare class A as a class template
with one template parameter, and then later redeclare class A as a
class template with two template parameters? Doesn't this contradict
the above because the name of the last class template ("A") is the
same as the name of the first class template ("A")?

I find the observed behaviour quite useful. But, if it turns out that
the observed behaviour is in conflict with the C++ standard, I'd
prefer not to write code that relies on it.

If anyone is able to clarify whether the observed behaviour is in
conflict with the standard, I would be very happy.
Jun 27 '08 #1
1 1589
* ctoo:
The following compiles and works with g++ 3.4.4 and Borland C++
Builder 6 update#4:

#include <iostream>
#include <vector>
#include <utility>

// declaration and definition for primary class template
template <class T>
class A
{
public:
void pr()
{
std::cout << "A<T>" << std::endl;
}
};

// declaration and definition for a class template specialization
template <class T>
class A<std::vector<T
{
public:
void pr()
{
std::cout << "A<std::vector<T" << std::endl;
}
};

// redeclaration (and definition) of the primary class template - this
time with two template parameters
template <class X, class Y>
class A<std::vector<std::pair<X,Y >
{
public:
void pr()
{
std::cout << "A<std::vector<std::pair<X,Y >" << std::endl;
}
};

int main(int , char* [])
{
A<int a;
A<std::vector<int b;
A<std::vector<std::pair<int,char c;

a.pr();
b.pr();
c.pr();
}

The output of the program is:
A<T>
A<std::vector<T
A<std::vector<std::pair<X,Y >

But, as stated in the C++ 1998 standard chapter 14, paragraph 5:
"A class template shall not have the same name as any other template,
class, function, object, enumeration, enumerator, namespace, or type
in the same scope (3.3), except as specified in (14.5.4)."

So, how is it possible to first declare class A as a class template
with one template parameter, and then later redeclare class A as a
class template with two template parameters? Doesn't this contradict
the above because the name of the last class template ("A") is the
same as the name of the first class template ("A")?

I find the observed behaviour quite useful. But, if it turns out that
the observed behaviour is in conflict with the C++ standard, I'd
prefer not to write code that relies on it.

If anyone is able to clarify whether the observed behaviour is in
conflict with the standard, I would be very happy.
Read up on "partial specialization" (both second and third definition of A are
partial specializations of the original primary template).

Cheers, & hth.,

- Alf
Jun 27 '08 #2

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

Similar topics

25
by: John Harrison | last post by:
This code fails to compile on Comeau C++ and VC++ 7.1 (with language extensions disabled) template <class T> struct B { T b; }; template <class T>
5
by: stephan beal | last post by:
Good afternoon, C++ers, This weekend i came across a fairly project-neutral trick which can be used to map C++ class names to their human-readable equivalents (a-la QObject's className()...
4
by: not.a | last post by:
When I compile the following code: //=================================================== template <typename T> class Foo { public: T x,y,z; }; template <typename T>
7
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++...
12
by: Steven T. Hatton | last post by:
In the following code, the class B will not compile if the function da is uncommented. I took exactly the same code, and turned it into templates. Never used the template parameter in anything but...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
5
by: Hartmut Sbosny | last post by:
Hello NG, I have a ordinary class `Base' with two functions `void foo()' and `void any()': class Base { public: Base() {} void foo() {} void any() {}
2
by: flopbucket | last post by:
Hi, If I have the following: template<class T> class A { public: int a; };
4
by: Wayne Shu | last post by:
Hey, guys. There is a problem. e.g. template <typename T> class foo { public: foo();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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,...

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.