Re: STL::list trouble in VC6
From: James Dennett (jdennett_at_acm.org)
Date: 01/25/05
- Next message: Chris \( Val \): "Re: STL::list trouble in VC6"
- Previous message: Mike Wahler: "Re: [C++] Need help with program please"
- In reply to: Chris \( Val \): "Re: STL::list trouble in VC6"
- Next in thread: Chris \( Val \): "Re: STL::list trouble in VC6"
- Reply: Chris \( Val \): "Re: STL::list trouble in VC6"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 24 Jan 2005 21:02:04 -0800
Chris ( Val ) wrote:
> "James Dennett" <jdennett@acm.org> wrote in message news:uQQId.16955$ru.613@fed1read07...
> | Chris ( Val ) wrote:
> | > "James Dennett" <jdennett@acm.org> wrote in message
> news:viDId.16774$ru.1323@fed1read07...
> | > | dwizard wrote:
>
> [snip]
>
> | > You can read the thread from a couple of years back here:
> | >
> http://www.google.com.au/groups?hl=en&lr=&threadm=aqdol3%2492bp5%241%40ID-110726.news.dfncis.de&rnum=1&prev=/groups%3Fas_q%3DRobert%2520W%2520Hand%26as_epq%3DWindow%26safe%3Dimages%26as_ugroup%3Dalt.comp.lang.learn.c-c%252B%252B%26as_uauthors%3DChris%2520(%2520Val%2520)%26lr%3D%26hl%3Den
> | >
> | > Out of curiosity, the following code will compile,
> | > unlike the OP's code ?
> |
> | Maybe... depends on the implementation of std::vector. If the
> | std::vector used a "small string"-like optimization, it might
> | not compile, as sizeof(CData) could be used in the definition
> | of std::(basic_)string.
>
> How exactly can sizeof(CData), be used
> in the definition of 'std::basic_string' ?
Pretty much however the implementor likes ;)
Making this up as I go along... (dropping the "warts"
which would be needed on names in a real implementation,
and assuming some extensions, variants of which might
make it into C++0x and some of which are already in
some compilers):
// Provides some storage for small strings...
template <typename char_type, size_t buffer_size>
struct basic_string_helper
{
char __attribute__((alignment, __align_of__(char_type)))
buffer[buffer_size];
static size_t const size = buffer_size;
};
// ..but not if the char_type is too big (this avoids
// instantiating the unspecialized template with a
// zero-sized array, which would be an error).
template <typename char_type>
struct basic_string_helper<char_type, 0>
{
static size_t const size = 0;
};
template <typename char_type, /* others */>
struct basic_string
: basic_string_helper<char_type, 16/sizeof(char_type)>
{
// much more here, using the base class for storage of
// small strings.
};
Now this is a lot of fun, thinking of useful ways that
basic_string could make use of the sizeof the char_type,
but drifts offtopic for this group. The idea above is
that basic_string could optimize by using storage for a
small number of characters while guarding against wasting
too much space. Given that some studies have shown that
in many applications typical strings are on average only
6 characters long, a lot of dynamic allocation overhead
can be avoided by having a small buffer embedded straight
into the string object as a member (in this case a member
of a base class to allow a point for specialization to
avoid division by zero for large char_type). The standard
is written to allow implementors to try all sorts of things
to make code smaller/faster/safer -- one of the trade-offs
being that you have to play within the rules, or your code
might not be portable (and indeed might run and not do
what you wanted).
-- James
- Next message: Chris \( Val \): "Re: STL::list trouble in VC6"
- Previous message: Mike Wahler: "Re: [C++] Need help with program please"
- In reply to: Chris \( Val \): "Re: STL::list trouble in VC6"
- Next in thread: Chris \( Val \): "Re: STL::list trouble in VC6"
- Reply: Chris \( Val \): "Re: STL::list trouble in VC6"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|