This is the mail archive of the
mailing list for the GCC project.
Re: using C++ STL containers in GCC/gfortran source code
- From: "N.M. Maclaren" <nmm1 at cam dot ac dot uk>
- To: Janus Weil <janus at gcc dot gnu dot org>
- Cc: gcc mailing list <gcc at gcc dot gnu dot org>, gfortran <fortran at gcc dot gnu dot org>, Jakub Jelinek <jakub at redhat dot com>
- Date: 16 Dec 2016 17:16:38 +0000
- Subject: Re: using C++ STL containers in GCC/gfortran source code
- Authentication-results: sourceware.org; auth=none
- References: <CAKwh3qgUN=mTFPg6Ew6Yk9RXgS3CttE7O3cGMQRcLKNqUOjBng@mail.gmail.com>
On Dec 16 2016, Janus Weil wrote:
What I'd like to know is: In the current state of things in GCC, is it
possible/reasonable to use any of the STL containers (like
std::vector, std::string, whatever) in GCC and its front ends (in
That question has two parts:
1) Is it technically possible at all? Are there drawbacks/pitfalls?
(In particular Jakub mentioned possible memory management issues, i.e.
xmalloc vs malloc etc.)
I can say something about this, which is not gcc-specific. The executive
summary is that there shouldn't be any problem if you KISS, but heaven
help you if you don't.
Not all containers etc. are equally problematic and, as expected, the
problem increase rapidly with complexity and how 'advanced' your code is.
For example, the interaction between sorting on a vector, allocation and
copying is, er, 'interesting' (and essentially unspecified).
There are a lot of restrictions and oddities to do with iterators and
any action that changes the container, which could cause trouble if
anyone working on the code isn't fully au fait with them.
The standard library is hopelessly ill-defined and ambiguous with regard
to parallel use of a single container so, if gcc wants to allow that, it
must be VERY conservative. A strict reading of the standard says that
this applies to any two containers derived from the same base class, but
that is almost certainly safe.
The de facto library specification keeps changing, not always in fully
compatible ways. For example, are containers allowed to count element
accesses? They used to be, that's for sure, but it's now doubtful - see
the previous paragraph for why.
In my experience, it is often easier (and sometimes less code) to write
your own container if you are pushing the boundaries. I doubt that gcc
will need n-D arrays, but that's an extreme example (and Boost is no