这是一个演示该问题的简短程序:
void main(string[] args)
{
unichar c = 'a';
string str_from_to_string = c.to_string(); // Warning
stdout.printf("Converted by unichar.to_string: \"%s\"\n", str_from_to_string);
}
这也会导致相同的警告:
void main(string[] args)
{
unichar c = 'a';
string str_from_template = @"$c"; // Warning
stdout.printf("Converted by string template: \"%s\"\n", str_from_template);
}
这是我得到的警告:
/home/mpiroc/Desktop/unicode_to_string/unicode_to_string.vala.c: In function ‘g_unichar_to_string’:
/home/mpiroc/Desktop/unicode_to_string/unicode_to_string.vala.c:26:2: warning: passing argument 2 of ‘g_unichar_to_utf8’ discards ‘const’ qualifier from pointer target type [enabled by default]
/usr/include/glib-2.0/glib/gunicode.h:684:11: note: expected ‘gchar *’ but argument is of type ‘const gchar *’
这是警告中提到的生成的c代码:
18 static gchar* g_unichar_to_string (gunichar self) {
19 gchar* result = NULL;
20 gchar* _tmp0_ = NULL;
21 gchar* str;
22 const gchar* _tmp1_;
23 _tmp0_ = g_new0 (gchar, 7);
24 str = (gchar*) _tmp0_;
25 _tmp1_ = str;
26 g_unichar_to_utf8 (self, _tmp1_);
27 result = str;
28 return result;
29 }
好像_tmp_
可能不应该被标记const
,但这是由 生成的valac
,不是我直接写的。难道我做错了什么?或者这是一个错误valac
?代码按预期运行,但我尽量避免警告。