Topic: Forward decalations for Standard C++ library types


Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1998/04/13
Raw View
Scott Meyers <smeyers@aristeia.com> writes:

>Nathan Myers wrote:
>
>  It is also specifically forbidden to forward-declare the basic_string<>
>  template, though you might not get a compile error.  The reason for that
>  is that implementers are allowed to add extra (defaulted) template
>  parameters, and your forward-declaration would then be wrong.
>
>Can you please point out the location of this prohibition in CD2 or the
>FDIS?  I can't find it, and since I've published words stating that you CAN
>perform this kind of forward declarartion, it's important to me that I get
>it right.

 |   17.4.3.1  Reserved names                          [lib.reserved.names]
 |
 | 1 It is undefined for a C++ program to add declarations  or  definitions
 |   to  namespace  std or namespaces within namespace std unless otherwise
 |   specified.

--
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3        |     -- the last words of T. S. Garp.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Scott Meyers <smeyers@aristeia.com>
Date: 1998/04/02
Raw View
Nathan Myers wrote:

  It is also specifically forbidden to forward-declare the basic_string<>
  template, though you might not get a compile error.  The reason for that
  is that implementers are allowed to add extra (defaulted) template
  parameters, and your forward-declaration would then be wrong.

Can you please point out the location of this prohibition in CD2 or the
FDIS?  I can't find it, and since I've published words stating that you CAN
perform this kind of forward declarartion, it's important to me that I get
it right.

Thanks,

Scott
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: dmitriy.kourtchikov@tms.tm3.com
Date: 1998/03/10
Raw View
Hello,

It appears, that string type from the Standard C++ library doesn't work as
an ordinary class. For example, the following code will cause a compilation
error:

   // File X.h
   // ...ifdef and other stuff

   class string; // forward declaration

   class X
   {
   public:
   // ...
       void f(string& s);

   };

   // File X.cpp
   //...
   #include X.h

   #include <string>     // Oops: Error: type redefinition
   //...

It looks like this error was caused by the use of typedef for string
definition. According to 7.1.3.3:

   In a given scope, a typedef specifier shall not be used to redefine
   the name of any type declared in that scope to refer to a different type.

Therefore, forward declaration can not be used for string since it is a
typedef, and the header file string should be included instead of the forward
declaration. Result - compilation time increase. The same problem exists for
other Standard C++ library typed defined using typedef. At the same time,
string can be used as a base class and behave almost as an ordinary class.

The following is just a general form of the problem:

    class Y; // forward declaration

    class X {};

    typedef X Y;  // Error: type redefenition

Is it a deficiency (trade-off) of C++ language or C++ library implementation?

Any comments from C++ gurus are very appreciated.

Dmitriy

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/03/11
Raw View
<dmitriy.kourtchikov@tms.tm3.com> wrote:
>It appears, that string type from the Standard C++ library doesn't work as
>an ordinary class. For example, the following code will cause a compilation
>error:
>
>   class string; // forward declaration
>   ...
>
>... forward declaration can not be used for string since it is a
>typedef, and the header file string should be included instead of the
>forward declaration. Result - compilation time increase. The same problem
>exists for other Standard C++ library typed defined using typedef. At
>the same time, string can be used as a base class and behave almost as
>an ordinary class.
>
>Is it a deficiency (trade-off) of C++ language or C++ library
>implementation?

Of course the committee would have liked to permit this.  However,
nobody knew any good way to implement it.

It is also specifically forbidden to forward-declare the basic_string<>
template, though you might not get a compile error.  The reason for that
is that implementers are allowed to add extra (defaulted) template
parameters, and your forward-declaration would then be wrong.

Probably there should have been more "<...fwd>" headers in the
standard.  With precompiled headers it will not make much difference.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]