是否在D中采用仅可在操作时间获得的信息而不分配记忆的“<>string?
我可能想做些什么的简单例子:
void renderText(string text) { ... }
void renderScore(int score)
{
char[16] text;
int n = sprintf(text.ptr, "Score: %d", score);
renderText(text[0..n]); // ERROR
}
采用这一方法,你发现了一个错误,因为<编码>>>文本代码>的斜体不能互换,因此不是<条码>(即<条码>可互换(char)[)。
我只能考虑三个方面:
- Cast the slice to a
string
. It works, but is ugly. - Allocate a new string using the slice. This works, but I d rather not have to allocate memory.
- Change
renderText
to take aconst(char)[]
. This works here, but (a) it s ugly, and (b) many functions in Phobos requirestring
, so if I want to use those in the same manner then this doesn t work.
其中无一例外。 我失踪了吗? 其他人如何处理这个问题?