C 数组大小作为函数?
可能的重复:
这个“数组大小”模板函数如何工作?
在 C++ (C++0x) 中是否有可能在没有宏的情况下实现 NARR
?
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));
一个简单的函数是不可能的,因为这样 []
就会丢失其大小信息并变成另一个指针:
size_t narr(sometype arr[]) { /* won't work */ }
模板?超载?魔法?
Possible Duplicate:
How does this “size of array” template function work?
Is there any possibility to implement NARR
without a macro in 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));
A simple function is not possible, because then the []
loses its size-information and becomes just another poiner:
size_t narr(sometype arr[]) { /* won't work */ }
Templates? Overloading? Magic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C++ 中这是可能的:
与宏解决方案相比,它的优点是,如果您尝试传递指向此的指针,编译器将发出令人讨厌的错误消息。
It is possible in C++:
The advantage of this over the macro solution is that the compiler will cough up a nasty error message if you try to pass a pointer to this.
在 C++0x 中:
如果您愿意,您也可以使用
std::begin(data)
来实现对称性或通用性[*]。无论如何,您可能可以使用初始值设定项列表而不是数组...[*] 尽管为了完全通用性,您可能应该这样做:
原因是新的“基于范围的 for 循环”语法相当于使用
begin
和end
没有限定,但使用std
作为 ADL 的关联命名空间。与std::swap
一样,类型的作者可能会提供旨在通过 ADL 找到的begin
和end
函数。他们可能应该提供begin()
和end()
成员函数,其中std::begin
和std::end
将调用。但是,如果他们提供的东西适用于基于范围的 for,但不适用于您的代码,那么您将不得不争论谁应该更改。In C++0x:
Also you can use
std::begin(data)
if you like, for symmetry or genericness[*]. And possibly you can use an initializer list instead of an array anyway...[*] Although for full genericness, you should probably do:
The reason is that the new "range-based for loop" syntax is equivalent to using
begin
andend
without qualification, but withstd
as an associated namespace for ADL. So as withstd::swap
, authors of types might providebegin
andend
functions that are intended to be found via ADL. They probably ought to providebegin()
andend()
member functions instead, whichstd::begin
andstd::end
will call. But if they provide something that works with ranged-based for, and doesn't work with your code, then you'll have to have an argument who should change.我现在无法测试这个,因为我已经离开了一段时间,最新的 GCC 4.6 拒绝编译,但是
constexpr
应该可以很快解决这个问题。当然,最好利用史蒂夫的建议来回避这个问题。I can't test this right now, because I've been away for a while and the latest GCC 4.6 just refused to compile, but
constexpr
should make short work of the question. Of course, it's better to sidestep this issue using Steve's suggestion.