可能重複:
How does this 「size of array」 template function work?大小 - 作爲一個函數的C數組?
是否有實現NARR
沒有宏C++(C++ 0x中)的可能性?
const static pair<string,int> data[] = {
{"Montag",1}, {"Dienstag",2}, {"Mittwoch",3}, {"Donnerstag",4},
{"Freitag",5}, {"Samstag",6}, {"Sonntag",7}
};
#define NARR(A) (sizeof(A)/sizeof(*A))
const static map<string,int> german_weekdays(data, data+NARR(data));
一個簡單的功能是不可能的,因爲那樣的話[]
失去了它的尺寸信息和成爲另一種poiner:
size_t narr(sometype arr[]) { /* won't work */ }
模板?超載?魔法?
http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx – 2011-04-05 07:57:14
對不起,愚蠢的問題也許是,但什麼是「NARR」代表比(其它「小丑」在德國)? – 2011-04-05 08:01:04
Duplicate:http://stackoverflow.com/questions/3368883/how-does-this-size-of-array-template-function-work/ – GManNickG 2011-04-05 08:38:30