Uniqueness of class template names

C

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.
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top