Topic: Q: Forward declaration of class templates?
Author: Leif =?iso-8859-1?Q?L=F6nnblad?= <leif@thep.lu.se>
Date: 1999/03/22 Raw View
Bruce Visscher wrote:
>
>
> It would be more syntactically correct to say:
>
> namespace std {
> template<typename T> class vector<T>;
> }
>
> However, this would is still be wrong. The vector template class also takes
> an allocator:
>
> namespace std {
> template <typename T/*whatever*/> class allocator;
> template <typename T, typename Allocator=allocator<T> > class vector;
> }
>
> But now when we try to #include <vector>, we'll have a problem since we've
> already provided a default value for the second template argument. This is
> illegal: default template arguments can only be declared in one (at least
> according to one compiler I've used, someone please correct me if this is
> wrong).
>
>
Wouldn't it be nice if the standard specified a header file with forward
declarations of all the standard containers. Just as we have <iosfwd> we
could have e.g. <stlfwd>. This would solve the problem of the original
poster. And some of mine as a matter of fact. Was this ever discussed by
the standardization committee?
--
--------------------------------------------------
Leif L nnblad, Department of Theoretical Physics 2
S lvegatan 14A, S-223 62 Lund, Sweden
phone: +46-46 2227780, fax: +46-46 2224438
e-mail: <Leif.Lonnblad@cern.ch>, <leif@thep.lu.se>
http://www.thep.lu.se/~leif
[ 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: Stephen.Clamage@Eng.Sun.COM (Steve Clamage)
Date: 1999/03/23 Raw View
Leif =?iso-8859-1?Q?L=F6nnblad?= <leif@thep.lu.se> writes:
>Wouldn't it be nice if the standard specified a header file with forward
>declarations of all the standard containers. Just as we have <iosfwd> we
>could have e.g. <stlfwd>. This would solve the problem of the original
>poster. And some of mine as a matter of fact. Was this ever discussed by
>the standardization committee?
The header structure of iostreams changed in the standard,
and class names like "ostream" became templates. To reduce
the impact on programs that expected to declare pointers
and references to stream objects but didn't need to operate
on streams, the committee created the <iosfwd> header.
There was no compatibility issue with other standard headers,
since they didn't exist before. The C++ committee discussed
the possibility, but didn't see much advantage in providing
"fwd" versions of the other headers. (You might disagree,
but that was what the committee decided.)
--
Steve Clamage, stephen.clamage@sun.com
---
[ 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: stanley@west.sun.com (Stanley Friesen [Contractor])
Date: 1999/03/18 Raw View
In article <36EF985D.5184198C@funcom.com>,
Jarl Christian Berentsen <chrisb@funcom.com> wrote:
>//#include <vector> // don't want to do this
>
>// forward declarating classes
>class std::vector<T>; // does not work
Try:
namespace std {
template <class T> class vector;
}
There is no requirement that a template declaration be a definition, so
the above should be the normal way to forward declare a class template.
That is one does a forward declaration just like a definition, but without
the definition part.
---
[ 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: James Kuyper <kuyper@wizard.net>
Date: 1999/03/18 Raw View
Jarl Christian Berentsen wrote:
>
> In order to avoid too much dependencies in headerfiles I want to do
> something like:
>
> //#include <vector> // don't want to do this
>
> // forward declarating classes
> class std::vector<T>; // does not work
> class Fool;
>
> class Foo
> {
> Fool* m_pcFool;
> std::vector<Fool>* m_pcFoolVector;
> };
>
> This fails. Does anybody know the correct way (if possible) to do this
> in a portable way?
What you would need to do (if it were legal) is something like:
namespace std
{
template<class T, class Allocator = allocator<T> >
class vector;
}
However, adding declarations to namespace std allows for undefined
behavior, so I wouldn't recommend that. Just do it the right way:
#include <vector>
Short cuts like what you're trying to do are just trouble waiting to
happen.
[ 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: Biju Thomas <bijuthom@ibm.net>
Date: 1999/03/18 Raw View
Jarl Christian Berentsen wrote:
>
> In order to avoid too much dependencies in headerfiles I want to do
> something like:
>
> //#include <vector> // don't want to do this
>
> // forward declarating classes
> class std::vector<T>; // does not work
> class Fool;
>
> class Foo
> {
> Fool* m_pcFool;
> std::vector<Fool>* m_pcFoolVector;
> };
>
> This fails. Does anybody know the correct way (if possible) to do this
> in a portable way?
Apart from Stanley's answer, you really shouldn't do this for the case
of standard library classes like vector. The C++ standard says that you
should use the include directive to bring in the standard library
contents. Using forward declaration may work, but, it is technically
illegal.
--
Best regards,
Biju Thomas
---
[ 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: "Daniel Parker" <danielp@no_spam.com>
Date: 1999/03/18 Raw View
Stanley Friesen [Contractor] <stanley@west.sun.com> wrote in article
<7cp5kq$d48@abyss.West.Sun.COM>...
>
> Try:
>
> namespace std {
> template <class T> class vector;
> }
>
Wouldn't work. Class vector has two template parameters (the second has a
default)
Regards,
Daniel Parker danielp@no_spam.anabasis.com
---
[ 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: Bruce Visscher <bvisscher@mindspring.com>
Date: 1999/03/18 Raw View
Jarl Christian Berentsen wrote:
> In order to avoid too much dependencies in headerfiles I want to do
> something like:
>
> //#include <vector> // don't want to do this
>
> // forward declarating classes
> class std::vector<T>; // does not work
It would be more syntactically correct to say:
namespace std {
template<typename T> class vector<T>;
}
However, this would is still be wrong. The vector template class also takes
an allocator:
namespace std {
template <typename T/*whatever*/> class allocator;
template <typename T, typename Allocator=allocator<T> > class vector;
}
But now when we try to #include <vector>, we'll have a problem since we've
already provided a default value for the second template argument. This is
illegal: default template arguments can only be declared in one (at least
according to one compiler I've used, someone please correct me if this is
wrong).
It also might be possible that an implementation supplies additional template
parameters (there was discussion on whether this was legal, I don't recall
what the consensus was).
There is also the mundane problem that even though "class allocator" and
"struct allocator" mean the same thing, a certain compiler written by a
company that owns a popular GUI based operating system doesn't know this.
The implementation could declare it either way.
In short, never forward declare a standard type.
> class Fool;
>
> class Foo
> {
> Fool* m_pcFool;
> std::vector<Fool>* m_pcFoolVector;
> };
>
> This fails. Does anybody know the correct way (if possible) to do this
> in a portable way?
Don't do this.
Now, hiding the implementation is a wonderful thing. But if you're going to
do this, go all the way. Don't let your clients know you are using vector at
all. See "Compilation Firewalls" (otherwise known as the pimpl_ idiom) on
Herb Sutters very useful GOTW site (http://www.cntc.com/resources/).
HTH,
Bruce Visscher
---
[ 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 ]