Postfix string literal alternative suggestion

Edward Diener eddielee_no_spam_here at tropicsoft.com
Sat Feb 9 15:26:58 PST 2008


Walter Bright wrote:
> Edward Diener wrote:
>> The string literal in D can have a postfix character of 'c', 'w', or 
>> 'd' for specifying the type of the literal. This notation is in the 
>> same spirit as the C++ prefix of L"..." to specify a wide character 
>> string. Such syntax does not play well with templates based on a 
>> character type.
>>
>> I would like to suggest instead the use of a cast(type) expression for 
>> string literals, perhaps something like 'string_cast(type)"some string 
>> literal"' where the type would have to be char, wchar, or dchar. This 
>> says to treat the literal as a particular type and would be a 
>> replacement for "some string literal"c|w|d notation. The reason I 
>> think my suggestion is superior is that one may have a template class 
>> or function in which one of the template parameters is a character 
>> type and then one can easily specify the template type using the 
>> notation suggested by me to coerce a string literal to the preferred 
>> type without having to know the type of the template parameter.
>>
> 
> You can already use the syntax:
> 
>     cast(string)"foo";
>     cast(wstring)"bar";
>     cast(dstring)"abc";
> 
> if you need to.

I would not have guessed that, but that is great since it could be used 
directly in templates where the type is a template paramater. I take 
back my suggestion since you have anticipated it.



More information about the Digitalmars-d mailing list