Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2003-01-19 12:59:48


On Sat, 18 Jan 2003 20:16:36 -0700, Greg Colvin
<Gregory.Colvin_at_[hidden]> wrote:

>At a meeting years ago I proposed to make string literals more
>useful as constant expressions, but we decided against that.
>As I recall part of the problem is that linkers are free to map
>the same literal string to different addresses in different
>compilation units.

I'm not sure what you are referring to by "using string-literals as
constant expressions". If you are alluding to making the array
referred to by a string-literal usable as a template argument, like in

   template <const char c[]>
   struct X {};

   X <"hello"> x;

then I'd like to make a comment. Currently C++ doesn't allow this.
Briefly, the problem is that a string-literal (which is not an object
per se, but an expression which *refers* to an object implicitly
created by the compiler) refers to an unnamed object and such an
object is not usable as a template argument. However it would be
perfectly possible to give it a compiler-generated name. Now, as you
say, it's not specified whether array objects corresponding to
identical string literals are collapsed together or not and thus you
don't know whether e.g. x and y below have the same type or not

   X <"hello"> x; // (*)
   X <"hello"> y;

But this is IMHO a problem only when it is a problem! :-) It's no
problem at all e.g. when I instantiate X<> just to make some sort of
processing on the string-literal itself (see below). And if I want to
be sure that different strings are instantiated I can explicitly use
arrays:

extern const char arr1[] = "hello";
extern const char arr2[] = "hello";

X<arr1> x;
X<arr2> y;

However processing on string-literals (example: counting the number of
occurrences of a given character) is anyhow impossible for other
reasons, mainly because you cannot cycle through the single characters
and obtain character constants. Thus, doesn't C++ lack a support for
true string rvalues?

Genny.


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