Boost logo

Boost :

From: Robert Klarer (klarer_at_[hidden])
Date: 2003-03-05 17:58:26


There's already been some discussion of this library under the thread
"Proposal: strings as template parameters," but static_string hasn't
been the subject of its own thread, so I'm starting this one. I'd like
to solicit opinions about this project. Is it worthwhile?

The purpose of the static_string library is to offer an alternative to
string literals and the standard type const std::string. A
static_string uses no dynamically allocated memory, and is more
efficient at execution time than either string literals or
basic_strings.

The syntax for declaring a static_string is unfortunate, but once it has
been declared, a static_string's interface is (almost*) the same as that
of a const std::string.

* A static_string does not allocate memory, so it has no allocator_type.
Similarly, since a static_string can only be used with characters of
type char, no traits_type is necessary.

//Example 1: declaring and using a static_string
//
#include <boost/static_string.hpp>

int main()
{
   typedef boost::static_string<'s', 't', 'a', 't', 'i', 'c', '_'>
StrType1;

   StrType1 str1;

   std::cout << "string one is: \'" << str1 << "\'\n";
   std::cout << "length of string one is: " << str1.length() << '\n';

   std::cout << "remove \'a\': ";
   std::ostream_iterator<char> oi(std::cout);
   std::remove_copy(str1.begin(), str1.end(), oi, 'a'); // prints
"sttic_"
   std::cout << std::endl;
}

A static_string cannot be modified, so all of the iterator types nested
within static_string behave as const_iterators. Internally, these
iterators are represented using pointers to const char, and are
therefore random access iterators.

Since the contents of a static_string are known at compile time, many
operations involving static_strings can be computed at program compile
time so that they are essentially free at program execution time
(assuming inlining).

//Example 2: free lunch?
//
#include <iostream>
#include <boost/static_string.hpp>

int main()
{
   typedef boost::static_string<'s', 't', 'a', 't', 'i', 'c', '_'>
StrType1;
   typedef boost::static_string<'s', 't', 'r', 'i', 'n', 'g'> StrType2;

   StrType1 str1;
   StrType2 str2;

   std::cout << "Strings one and two are "
             << (str1.compare(str2) ? "not " : "") // cost of this call
is nil
             << "equal" << std::endl;
}

As well, number of metafunctions are defined for static_string. Any
operation that be performed by means of a call to one of static_string's
member functions can be performed by one of these metafunctions.

//Example 3: metafunction interface
//
#include <iostream>
#include <boost/static_string.hpp>

int main()
{
   typedef boost::static_string<'s', 't', 'a', 't', 'i', 'c', '_'>
StrType1;
   typedef boost::static_string<'s', 't', 'r', 'i', 'n', 'g'> StrType2;

   typedef StrType1::concat_static::apply<StrType2>::value StrType3;
   std::cout << "Concatenation of strings one and two: "
             << StrType3::c_str_static::apply::value
             << std::endl;
}

Thanks for considering my proposal.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk