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

[Q] templates & nested class

0 views
Skip to first unread message

be...@geometrix.de

unread,
Jan 13, 1997, 3:00:00 AM1/13/97
to


hello all,

I got a problem compiling the following code:

----------------------------------------------------------

template<class T1>
class C {
public:
typedef T1::num num;
C() {}
};

template<class T2>
class Outer {
public:
Outer() {}

class Inner {
public:
typedef T2::anum num;
B() {}
num n() const { return num(0);}
};

void n() const { C<Inner> c; }
};


class Type2 {
public:
typedef int anum;
};

int main() {
Outer<Type2> a;
return 0;
}
------------------------------------------------------------------

compiling with Sun CC V 4.1 gives the following error:
"test-nested-template2.C", line 16: Error: Redefining C::num after use in an outer scope.
"test-nested-template2.C", line 31: Where: While specializing "Outer<Type2>".
"test-nested-template2.C", line 31: Where: Specialized in non-template code.
1 Error(s) detected.

compiling with g++ 2.7.2 works fine.

If I change anum to num, it works.
is this a bug in Sun's CC ?

--guntram

------------------------------------------------------------------
Dipl.-Math. Guntram Berti | voice: ++49 +355 69 37 17
| fax : ++49 +355 69 24 02
LS Wissenschaftliches Rechnen | email: be...@math.tu-cottbus.de
und Numerische Mathematik | WWW: http://math-s.math.tu-cottbus.de/berti/
Institut f"ur Mathematik |
BTU Cottbus | Room 313, O__
Universit"atsplatz 3-4 | Lehrgeb"aude 1 c/ /'_
D-03044 Cottbus | (*) \(*)
------------------------------------------------------------------
--
------------------------------------------------------------------
Dipl.-Math. Guntram Berti | voice: ++49 +355 69 37 17
| fax : ++49 +355 69 24 02
LS Wissenschaftliches Rechnen | email: be...@math.tu-cottbus.de
und Numerische Mathematik | WWW: http://math-s.math.tu-cottbus.de/berti/
Institut f"ur Mathematik |
BTU Cottbus | Room 313, O__
Universit"atsplatz 3-4 | Lehrgeb"aude 1 c/ /'_
D-03044 Cottbus | (*) \(*)
------------------------------------------------------------------

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]


Deepak Goel

unread,
Jan 15, 1997, 3:00:00 AM1/15/97
to

It works fine with Microsoft Visual C++ 4.x. So definitely it seems like a
bug in CC.


Deepak Goel

Renato Ghica

unread,
Jan 24, 1997, 3:00:00 AM1/24/97
to

Deepak Goel wrote:
>
> It works fine with Microsoft Visual C++ 4.x. So definitely it seems like a
> bug in CC.
>
> Deepak Goel
>

MSVisual C++ is in error, as usual. Get another compiler. The sparc
compiler is correct, see below. Also, CC makes reference to this fact by
saying:

-------------------------------------------------------------------------

1.3.6 Redefining AAAA after use in BBBB

C++ contains rules that prohibit any redefinition of an outer
scope name that has been used in the class. For example:

typedef int TI;
class C {
TI iv;
float TI;
};

ps: do all version of borland c++ catch this, or just the ones after
4.5?

-rg
--------------------------------------------------------------------------
9.3 Scope rules for classes [class.scope0]

1 The following rules describe the scope of names declared in classes.

1)The scope of a name declared in a class consists not only of the
declarative region (_basic.scope.class_) following the name's
declarator, but also of all function bodies, default arguments,
and constructor initializers in that class (including such things
in nested classes).

**************************************************************************
2)A name N used in a class S shall refer to the same declaration
when re-evaluated in its context and in the completed scope of S.
**************************************************************************

3)If reordering member declarations in a class yields an alternate
valid program under (1) and (2), the program's behavior is unde-
fined.

4)A declaration in a nested declarative region hides a declaration
whose declarative region contains the nested declarative region.

5)A declaration within a member function hides a declaration whose
scope extends to or past the end of the member function's class.

6)The scope of a declaration that extends to or past the end of a
class definition also extends to the regions defined by its member
definitions, even if defined lexically outside the class (this
includes static data member initializations, nested class defini-
tions and member function definitions (that is, the parameter-
declaration-clause including default arguments
(_dcl.fct.default_), the member function body and, for constructor
functions (_class.ctor_), the ctor-initializer
(_class.base.init_)).


--

----------------------------------------------------------------
Any comments or statements made are not necessarily those of
J.P. Morgan & Co. Incorporated, its subsidiaries or affiliates.
----------------------------------------------------------------

0 new messages