這裏是一個簡短的程序來演示這個問題:爲什麼在將unichar轉換爲vala中的字符串時會出現編譯警告?
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
中的一個錯誤?代碼的功能與預期的一樣,但我儘可能避免發出警告。
你也可以''valac -X -w -o unicode_to_string unicode_to_string.vala'。 --Xcc/-X會告訴valac將提供的參數傳遞給CC。 – nemequ