Topic: forwards for classes inside namespaces
Author: John Kewley <kewley@cscs.ch>
Date: Mon, 30 Oct 2000 20:14:51 GMT Raw View
Francis Glassborow wrote:
> >I found that for a class C inside namespace NS, I could not write:
> >
> >// instead of #include "C.hxx"
> >class NS::C;
>
> But this might be an attempt to forward declare a class C nested in a
> class NS and so is ambiguous.
But other posters have stated that forwarded declaring a nested
type is illegal, how can it therefore be ambiguous?
Cheers,
--
John M. Kewley Tel: +41 (0) 91 610 8248
Senior Scientist Fax: +41 (0) 91 610 8282
Swiss Center for Scientific Computing mailto:John.Kewley@cscs.ch
Via Cantonale, CH-6928 Manno, Switzerland http://www.cscs.ch/%7Ekewley/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: John Wiegley <jwiegley@inprise.com>
Date: Mon, 30 Oct 2000 20:15:41 GMT Raw View
>>>>> On Fri Oct 27, John writes:
> So should: namespace NS {} class NS::C;
> be OK?
When the compiler sees NS::C, NS must exist. Also, C must exist
within NS. That's why just "class NS::C" doesn't work (because NS is
unknown), and also why the above piece of code will not work (because
C does not exist within NS).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: John Kewley <kewley@cscs.ch>
Date: Tue, 31 Oct 2000 16:46:34 GMT Raw View
Gabriel Dos Reis wrote:
>
> John Kewley <kewley@cscs.ch> writes:
> | So should:
> |
> | namespace NS {}
> | class NS::C;
> | be OK
> If you write that, why can't you just write:
>
> namespace NS { class C; }
The example that brought this up made use of a variety of classes
from NS.
I would prefer :
class NS::C;
class NS::D;
class NS::E;
class NS::F;
namespace NS
{
... more stuff for NS
}
to
namespace NS
{
class C;
class D;
class E;
}
namespace NS
{
... more stuff for NS
}
My alternative suggestion was:
namespace NS;
class NS::C;
class NS::D;
class NS::E;
class NS::F;
Although that would not have made much sense in this example since the
name NS had already been introduced as a namespace in a previously included
header.
--
John M. Kewley Tel: +41 (0) 91 610 8248
Senior Scientist Fax: +41 (0) 91 610 8282
Swiss Center for Scientific Computing mailto:John.Kewley@cscs.ch
Via Cantonale, CH-6928 Manno, Switzerland http://www.cscs.ch/%7Ekewley/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Gabriel Dos Reis <gdr@merlin.codesourcery.com>
Date: Mon, 30 Oct 2000 19:48:26 GMT Raw View
John Kewley <kewley@cscs.ch> writes:
| Francis Glassborow wrote:
| >
| > In article <39F6F0A2.BDC6AB8C@cscs.ch>, John Kewley <kewley@cscs.ch>
| > > ...
| > >I found that for a class C inside namespace NS, I could not write:
| > >
| > >// instead of #include "C.hxx"
| > >class NS::C;
| >
| > But this might be an attempt to forward declare a class C nested in a
| > class NS and so is ambiguous.
|
| So should:
|
| namespace NS {}
| class NS::C;
If you write that, why can't you just write:
namespace NS { class C; }
?
I bet you'll have less characters to type ;-)
--
Gabriel Dos Reis gdr@codesourcery.com
CodeSourcery, LLC http://www.codesourcery.com
http://www.codesourcery.com/gcc-compile.shtml
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: John Kewley <kewley@cscs.ch>
Date: 2000/10/25 Raw View
In order to reduce a friends compile times recently I decided to forward
declare some of the types he used rather than include thteir headers.
I found that for a class C inside namespace NS, I could not write:
// instead of #include "C.hxx"
class NS::C;
I had to write:
// instead of #include "C.hxx"
namespace NS
{
class C;
}
Was my first syntax contrary to the standard or was it the
compiler that was nonconforming?
In a similar vein, I considered the typedefs, unions and enums
that were also part of the namespace and tried to forward declare them,
unfortunately unions need to be forward declared with the union keyword
(I think that struct and class are interchangeable for forward declaring
structs and classes). I haven't manage to forward declare typedefs
or enums. It is a shame that the typename variable cannot be
used to unify this situation.
I have noticed that there is a header <iosfwd> that can be used
to forward declare the commonly used types from <iostream>, is it
considered good practise to provide this sort of header for those who only
need to have access to the types, but don't know whether this version
of the library has typedefs or classes for the types being used.
Cheers,
--
John M. Kewley Tel: +41 (0) 91 610 8248
Senior Scientist Fax: +41 (0) 91 610 8282
Swiss Center for Scientific Computing mailto:John.Kewley@cscs.ch
Via Cantonale, CH-6928 Manno, Switzerland http://www.cscs.ch/%7Ekewley/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 26 Oct 2000 14:36:18 GMT Raw View
In article <39F6F0A2.BDC6AB8C@cscs.ch>, John Kewley <kewley@cscs.ch>
writes
>In order to reduce a friends compile times recently I decided to forward
>declare some of the types he used rather than include thteir headers.
>
>I found that for a class C inside namespace NS, I could not write:
>
>// instead of #include "C.hxx"
>class NS::C;
But this might be an attempt to forward declare a class C nested in a
class NS and so is ambiguous.
>
>I had to write:
>
>// instead of #include "C.hxx"
>namespace NS
>{
> class C;
>}
Which this is not, though you could have written it all on one line:)
>
>Was my first syntax contrary to the standard or was it the
>compiler that was nonconforming?
The former
>
>
>In a similar vein, I considered the typedefs, unions and enums
>that were also part of the namespace and tried to forward declare them,
>unfortunately unions need to be forward declared with the union keyword
>(I think that struct and class are interchangeable for forward declaring
>structs and classes).
Supposedly, but I understand that one well known compiler has had
difficulty with mixing them.
>I haven't manage to forward declare typedefs
>or enums.
Because a typedef is an aliasing process and an enums are not usually
used in circumstances where references and pointers are all you need.
>It is a shame that the typename variable cannot be
>used to unify this situation.
So write up a proposal and find someone to champion it on the standard
committee. Even better persuade your compiler implementor that it would
make a sensible extension.
>
>I have noticed that there is a header <iosfwd> that can be used
>to forward declare the commonly used types from <iostream>, is it
>considered good practise to provide this sort of header for those who only
>need to have access to the types, but don't know whether this version
>of the library has typedefs or classes for the types being used.
iosfwd is there to meet a very special need, the coupling between
different parts of the Standard C++ Library is so high that forward
declaration of library types is very dependant on the library
implementation.
>
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Bo-Staffan Lankinen" <bSoP_AsMteSfUfCaKnS_lankinen@hotmail.com>
Date: Thu, 26 Oct 2000 19:02:43 GMT Raw View
> But this might be an attempt to forward declare a class C nested in a
> class NS and so is ambiguous.
Let's assume NS is a class and C is a nested class of NS. Is it possible to
forward declare C?
class NS;
class NS::C;
VC6.0 doesn't allow that and I can't come up with a good reason why. Is
VC6.0 non-conforming?
Bo-Staffan
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: John Kewley <kewley@cscs.ch>
Date: Fri, 27 Oct 2000 13:26:54 GMT Raw View
Francis Glassborow wrote:
>
> In article <39F6F0A2.BDC6AB8C@cscs.ch>, John Kewley <kewley@cscs.ch>
> > ...
> >I found that for a class C inside namespace NS, I could not write:
> >
> >// instead of #include "C.hxx"
> >class NS::C;
>
> But this might be an attempt to forward declare a class C nested in a
> class NS and so is ambiguous.
So should:
namespace NS {}
class NS::C;
be OK?
> >It is a shame that the typename variable cannot be
> >used to unify this situation.
>
> So write up a proposal and find someone to champion it on the standard
> committee. Even better persuade your compiler implementor that it would
> make a sensible extension.
It sounds a bit like Modula-2's abstract types!
> iosfwd is there to meet a very special need, the coupling between
> different parts of the Standard C++ Library is so high that forward
> declaration of library types is very dependant on the library
> implementation.
Is it the only header that is made up only of such forward declared
types?
Cheers,
--
John M. Kewley Tel: +41 (0) 91 610 8248
Senior Scientist Fax: +41 (0) 91 610 8282
Swiss Center for Scientific Computing mailto:John.Kewley@cscs.ch
Via Cantonale, CH-6928 Manno, Switzerland http://www.cscs.ch/%7Ekewley/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Gabriel Dos Reis <gdr@merlin.codesourcery.com>
Date: Fri, 27 Oct 2000 13:29:43 GMT Raw View
"Bo-Staffan Lankinen" <bSoP_AsMteSfUfCaKnS_lankinen@hotmail.com> writes:
| > But this might be an attempt to forward declare a class C nested in a
| > class NS and so is ambiguous.
|
| Let's assume NS is a class and C is a nested class of NS. Is it possible to
| forward declare C?
No.
Contrary to classes, namespaces can be reopen and they can spread
over several files. So class-name declarations in namespaces should
not cause any difficulty per se.
--
Gabriel Dos Reis gdr@codesourcery.com
CodeSourcery, LLC http://www.codesourcery.com
http://www.codesourcery.com/gcc-compile.shtml
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Bo-Staffan Lankinen" <bSoP_AsMteSfUfCaKnS_lankinen@hotmail.com>
Date: Fri, 27 Oct 2000 18:09:25 GMT Raw View
> No.
>
> Contrary to classes, namespaces can be reopen and they can spread
> over several files. So class-name declarations in namespaces should
> not cause any difficulty per se.
Ok, I know that, but why isn't it legal to forward declare a class that is
nested in another class? Several times have I been forced to include the
header with the class definition instead of just doing a forward declaration
of the class.
Bo-Staffan
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Dennett <james@evtechnology.com>
Date: Sat, 28 Oct 2000 17:29:02 GMT Raw View
Bo-Staffan Lankinen wrote:
> > No.
> >
> > Contrary to classes, namespaces can be reopen and they can spread
> > over several files. So class-name declarations in namespaces should
> > not cause any difficulty per se.
>
> Ok, I know that, but why isn't it legal to forward declare a class that is
> nested in another class? Several times have I been forced to include the
> header with the class definition instead of just doing a forward declaration
> of the class.
>
It's not legal to forward declare _anything_ which is nested in a class. The
only thing which can state the existence of something inside a class is the
definition for that class; class is a closed concept.
I think this helps to avoid violations of the ODR.
// forward.h
class A;
class A::B;
// real.h
class A {
typedef bool B; // out of sync with the forwarding header file
}
With the current rules, we know that updating the class definition will catch
all declarations of B (and of any nested names). I think that the current rules
help to make a class more self-contained.
-- James Dennett <jdennett@acm.org>
--
Sessami is a trademark of Escape Velocity Technology Mobile Services Limited.
All information contained in this e-mail is confidential and for the use of
the addressee only. If you receive this message in error please notify.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]