Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-01-19 14:01:16


>From: "Gennaro Prota" <gennaro_prota_at_[hidden]>

> 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;

You could avoid this problem by encoding the text string in the value used
to represent the string literal. That would ensure that all template
instantiations, using the same string literal, regardless of translation
unit, have the same type. However, you would still have the issue, as Greg
says, here, that the literals could have different addresses in different
TUs, so that if you took their address, you'd get different values, despite
being "the same" literal.

Regards,

Terje


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