Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Elaborated type specifier as template argument

4 views
Skip to first unread message

GMan

unread,
Aug 18, 2010, 8:01:17 PM8/18/10
to
Consider the following class:

template <typename T> foo {};

Is used by two translation units:

// TU1
typedef foo<struct incomplete_type> tu1;

// TU2
typedef foo<struct incomplete_type> tu2;

Are tu1 and tu2 guaranteed to refer to distinct types? Why?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Daniel Krügler

unread,
Aug 19, 2010, 5:54:37 AM8/19/10
to
On 19 Aug., 02:01, GMan <gmanni...@gmail.com> wrote:
> Consider the following class:
>
> template <typename T> foo {};

For the following discussion I read this as:

template <typename T> struct foo {};

> Is used by two translation units:
>
> // TU1
> typedef foo<struct incomplete_type> tu1;
>
> // TU2
> typedef foo<struct incomplete_type> tu2;
>
> Are tu1 and tu2 guaranteed to refer to distinct types? Why?

It depends on the context, in which both typedef
occur. If both are in the same namespace (including
the global namespace), they should refer to the same
type. I read this from [basic.scope.pdecl]/6 of the current
draft (It should apply to C++03 as well, the corresponding
paragraph is [basic.scope.pdecl]/5):

The point of declaration of a class first declared in an elaborated-
type-
specifier is as follows:
— for a declaration of the form
class-key attribute-specifieropt identifier ;
the identifier is declared to be a class-name in the scope that
contains
the declaration, otherwise
— for an elaborated-type-specifier of the form
class-key identifier
if the elaborated-type-specifier is used in the decl-specifier-seq or
parameter-declaration-clause of a function defined in namespace
scope,
the identifier is declared as a class-name in the namespace that
contains the declaration; otherwise, except as a friend declaration,
the
identifier is declared in the smallest non-class, non-function-
prototype
scope that contains the declaration. [ Note: these rules also apply
within templates. —end note ] [ Note: other forms of elaborated-type-
specifier do not declare a new name, and therefore must refer to an
existing type-name. See 3.4.4 and 7.1.6.3. —end note ]

You example belongs to bullet 2 and falls into the "otherwise" slot.
The effects of the wording are as if the above typedefs were
rewritten as

// TU1
struct incomplete_type;
typedef foo<incomplete_type> tu1;

// TU2
struct incomplete_type;
typedef foo<incomplete_type> tu2;

HTH & Greetings from Bremen,

Daniel Krügler

CornedBee

unread,
Aug 19, 2010, 10:41:51 PM8/19/10
to
On Aug 18, 5:01 pm, GMan <gmanni...@gmail.com> wrote:
> Consider the following class:
>
> template <typename T> foo {};
>
> Is used by two translation units:
>
> // TU1
> typedef foo<struct incomplete_type> tu1;
>
> // TU2
> typedef foo<struct incomplete_type> tu2;
>
> Are tu1 and tu2 guaranteed to refer to distinct types? Why?

Equivalence of types in C++ is based on name alone. Assuming these
declarations are in the same namespace, they are guaranteed to refer
to the same type.

Sebastian

Daniel Krügler

unread,
Aug 20, 2010, 5:17:57 AM8/20/10
to
On 19 Aug., 11:54, Daniel Krügler <daniel.krueg...@googlemail.com>
wrote:

Here a follow-up to complete the logic conclusion
that incomplete_type within TU1 and TU2 shall denote
the same for both TU1 and TU2:

1) Both types have external linkage as of 3.5 [basic.link]/4:

"A name having namespace scope has external linkage if
it is the name of:
[..]
— a named class (Clause 9) [..]"

2) Types of external linkage with the same name shall be the
same types, see 3.5 [basic.link]/9:

"Two names that are the same (Clause 3) and that are
declared in different scopes shall denote the same
variable, function, type, enumerator, template or
namespace if
— both names have external linkage [..]"

Johannes Schaub (litb)

unread,
Aug 22, 2010, 4:14:23 PM8/22/10
to
Daniel Krügler wrote:

As far as i can see, nothing is said about what namespace or class that
class is a member of, which is subject of the second bullet:

"- both names refer to members of the same namespace or to members, not by
inheritance, of the same class; "

0 new messages