string types: const(char)[] and cstring

Bill Baxter dnewsgroup at billbaxter.com
Fri May 25 22:59:46 PDT 2007


Walter Bright wrote:
> Under the new const/invariant/final regime, what are strings going to be 
> ? Experience with other languages suggest that strings should be 
> immutable. To express an array of const chars, one would write:
> 
>     const(char)[]
> 
> but while that's clear, it doesn't just flow off the keyboard. Strings 
> are so common this needs an alias, so:
> 
>     alias const(char)[] cstring;
> 
> Why cstring? Because 'string' appears as both a module name and a common 
> variable name. cstring also implies wstring for wchar strings, and 
> dstring for dchars.
> 
> String literals, on the other hand, will be invariant (which means they 
> can be stuffed into read-only memory). So,
>     typeof("abc")
> will be:
>     invariant(char)[3]
> 
> Invariants can be implicitly cast to const.
> 
> In my playing around with source code, using cstring's seems to work out 
> rather nicely.
> 
> So, why not alias cstring to invariant(char)[] ? That way strings really 
> would be immutable. The reason is that mutables cannot be implicitly 
> cast to invariant, meaning that there'd be a lot of casts in the code. 
> Casts are a sledgehammer, and a coding style that requires too many 
> casts is a bad coding style.


So basically most functions that take a char[] now would be changed to 
take a cstring in your thinking?

Is it also correct to say that cstring would be used in the places where 
one would use const char* or const std::string&  in C++?

If so that sounds ok to me.  But about the naming ... I have to agree 
that my first thought was "C compatible null terminated string" too, 
like std::string's  .c_str() method in C++.  I can probably live with 
that but I don't like the inconsistency with c/w/d.

Plain 'string' really does make the most sense.
plain   'w'     'd'
======= =====   =====
char    wchar   dchar
string  wstring dstring

It wouldn't be quite as bad if you uniformly apply the 'c' to all of 
them (using 'c' as a flag for constness):
plain   'w'      'd'
======= =====    =====
char    wchar    dchar
cstring wcstring dcstring
or
cstring cwstring cdstring

Some people already alias char[] to string.  As far as I've heard they 
haven't run into conflicts with the module name, or with people naming 
variables 'string'.

Question: if you have an alias like
alias char[] string;

'const string' automatically applies const to both the char and the [], 
right?  Is that something to be worried about?

--bb



More information about the Digitalmars-d-announce mailing list