By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

Uniqueness of class template names

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
* 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.