469,923 Members | 1,566 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

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 1439
* 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by John Harrison | last post: by
4 posts views Thread by not.a | last post: by
7 posts views Thread by Thomas Matthews | last post: by
12 posts views Thread by Steven T. Hatton | last post: by
21 posts views Thread by Jon Slaughter | last post: by
5 posts views Thread by Hartmut Sbosny | last post: by
2 posts views Thread by flopbucket | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.