473,402 Members | 2,046 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,402 software developers and data experts.

Linkage of templates

I read Nicolai and David's book "C++ templates" and they mention, on
page 99, that every template must have a name that is unique within its
scope. The following is the example they gave:

int C;

class C; //ok: class names & nonclass names are in a different
"space"

int X;

template < typename T>
class X; //Error:conflict with variable X

struct S;

template <typename T>
class S; //Error:conflict with struct S

Can anyone explain why does template names must be unique ? What is the
rationale behind this restriction imposed by the language ? Since int C
and class C can share the same name, I don't see why the name 'X' and
'S' above can raise any conflict.

Thanks.

Apr 19 '06 #1
2 2310
Chor Lit wrote:
I read Nicolai and David's book "C++ templates" and they mention, on
page 99, that every template must have a name that is unique within its
scope. The following is the example they gave:

int C;

class C; //ok: class names & nonclass names are in a different
"space"

int X;

template < typename T>
class X; //Error:conflict with variable X

struct S;

template <typename T>
class S; //Error:conflict with struct S

Can anyone explain why does template names must be unique ? What is the
rationale behind this restriction imposed by the language ? Since int C
and class C can share the same name, I don't see why the name 'X' and
'S' above can raise any conflict.


The short answer to this is "Because the standard says so".

However, perhaps some more insight would be that there is some level of
ambiguity and potential errors you can eliminate.

A.

struct S;
template <typename T> struct S
{
S & operator=( const S & ); // which S ?
};

B.

int X;

template < typename T>
struct X
{
// which X ?
X()
{
int y = X;
}
};

Apr 19 '06 #2
Thanks Gianni.

I think this solve the ambiguity. But I am still not sure whether it is
valid.

A.

struct S;
template <typename T> struct S
{
::S & operator=( const ::S & ); // the global 'S', not struct S
};
B.

int X;

template < typename T>
struct X
{
// must be struct X , int X in this context is impossible
X()
{
int y = X; // no ambiguity.
}

}

I can't think of a counter example to support template name uniqueness
in a scope. Anyone can think of the reason ?

Thanks.

Apr 19 '06 #3

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

Similar topics

9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
4
by: Tron Thomas | last post by:
I have read that anonymous namespaces are preferred in C++ over declaring global statics because the namespace can hide information from other translation units and at the same time provide...
20
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
3
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
3
by: martinbriefcase | last post by:
Compiling a program using ACE + MSVC++8, got lots of errors and some are listed as below: Error 2 error C2894: templates cannot be declared to have 'C' linkage c:\program files\microsoft visual...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
12
by: Taras_96 | last post by:
Hi everyone, AFAIK external linkage allows you to refer to variables/functions outside of the current translation unit. A variable in an unnamed namespace is similar to declaring a static...
4
by: Immortal_Nephi | last post by:
You have written several global functions inside header code. Then, you create either static library or dynamic linked library. All global functions can be reuseable to the main() function when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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...

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.