Topic: Forward declaration of types in std
Author: nimel@my-dejanews.com
Date: 1998/10/05 Raw View
Which way does the standard mandate to forward declare types that belongs to
namespace std. In particular I want to declare the streaming operator <<
for some class in a headerfile, without having to include <iostream>.
Perhaps <iosfwd> can solve this particular problem but what about a more
general case where the type is not declared in that header file.
Example:
namespace std { class ostream; }
// Is the line above legal according to the standard?
// If not, what are the alternatives?
class Foo
{
public:
friend std::ostream& operator<< (std::ostream&, const Foo&);
//...
};
/Niklas Mellin
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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/10/05 Raw View
<nimel@my-dejanews.com> wrote:
>
>Which way does the standard mandate to forward declare types that belongs to
>namespace std. In particular I want to declare the streaming operator <<
>for some class in a headerfile, without having to include <iostream>.
It is entirely forbidden. If you declare a name in std:: without
first including the header that defines it, your program is broken.
The compiler is not required to report the error.
There is only one way to get a standard name into your program, and
that is via #include. This is not an idle restriction; if you violate
it your code may break without warning.
>Perhaps <iosfwd> can solve this particular problem but what about a more
>general case where the type is not declared in that header file.
Then you can include <ios>, or <ostream>, or <istream>, instead.
--
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 ]
Author: "Alfred Kellner" <alfkellner@magnet.at>
Date: 1998/10/05 Raw View
nimel@my-dejanews.com wrote:
>
> Which way does the standard mandate to forward declare types that belongs to
> namespace std. In particular I want to declare the streaming operator <<
> for some class in a headerfile, without having to include <iostream>.
>
> Perhaps <iosfwd> can solve this particular problem but what about a more
> general case where the type is not declared in that header file.
>
> Example:
> namespace std { class ostream; }
There is no "class ostream;" in namespace std {}.
<iosfwd> defines std::ostream as
namespace std {
typedef basic_ostream<char> ostream;
}
> // Is the line above legal according to the standard?
Maybe ... probably not.
<quote>
17.3.1.2 Requrements [lib.structure.requirements]
The library can be extended by a C++ program. Each clause as
applicable describes the requirements that such extensions must meet.
</quote>
You didn't want to extend the library, did you ?
--ALfred
> // If not, what are the alternatives?
>
> class Foo
> {
> public:
> friend std::ostream& operator<< (std::ostream&, const Foo&);
> //...
> };
[ 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 ]