I would like to have the following method as a generic method for any array,
我希望将下面的方法作为任何数组的通用方法,
int arrayLength(`anyType` array[])
{
return sizeof(array) / sizeof(array[0]);
}
However it appears C++ doesn't allow any ambiguity of types at all,
然而,c++似乎根本不允许类型有任何歧义,
why is this, and how should I go about getting around it?
为什么会这样,我该怎么绕过它呢?
3 个解决方案
#1
3
Because types have to be pushed onto the stack and then popped back off, and the sizeof
one type is not equal to the sizeof
another type.
因为类型必须被推到堆栈上,然后再弹出,一个类型的大小不等于另一个类型的大小。
If the size of types being passed on the stack between functions is not fixed or known in advance, how can the compiler compile a function?
如果函数之间在堆栈上传递的类型的大小没有事先确定或已知,编译器如何编译函数?
The solutions to this problem -- as others have noted -- is templates and macros, both of which dynamically generate code -- which is then, in turn, compiled -- at compile-time, appearing to "solve" the problem, but really only obviating or distracting you from it by offloading the work onto the compiler.
这个问题的解决方案——当别人注意到——模板和宏,这两个动态生成代码,然后,反过来,在编译时,编译出现来“解决”问题,但实际上只有私企或分散你把工作在编译器上。
#2
1
In Visual C++ there's a __countof()
construct that does the same. It's implemented as a template for C++ compiling and as a macro for C. The C++ version errors out if used on a pointer (as opposed to a true array), the C version does not.
在visualc++中,有一个__countof()构造也执行相同的操作。它被实现为c++编译的模板,作为C的宏,如果在指针上使用c++版本错误(与真正的数组相反),则C版本没有。
#3
1
I think what you're really asking is "Why does C++ insist on static typing?"
我认为您真正想问的是“为什么c++坚持使用静态类型?”
The answer: because it's easier to write a compiler that generates small, fast programs if the language uses static typing. And that's the purpose of C++: creating small, fast programs whose complexity would be relatively unmanageable if written in C.
答案是:因为如果语言使用静态类型,编写生成小型、快速程序的编译器会更容易。这就是c++的目的:创建小的、快速的程序,如果用C编写,其复杂性将相对难以管理。
When I say "small", I'm including the size of any required runtime libraries.
当我说“小”时,我包含了任何所需的运行时库的大小。
#1
3
Because types have to be pushed onto the stack and then popped back off, and the sizeof
one type is not equal to the sizeof
another type.
因为类型必须被推到堆栈上,然后再弹出,一个类型的大小不等于另一个类型的大小。
If the size of types being passed on the stack between functions is not fixed or known in advance, how can the compiler compile a function?
如果函数之间在堆栈上传递的类型的大小没有事先确定或已知,编译器如何编译函数?
The solutions to this problem -- as others have noted -- is templates and macros, both of which dynamically generate code -- which is then, in turn, compiled -- at compile-time, appearing to "solve" the problem, but really only obviating or distracting you from it by offloading the work onto the compiler.
这个问题的解决方案——当别人注意到——模板和宏,这两个动态生成代码,然后,反过来,在编译时,编译出现来“解决”问题,但实际上只有私企或分散你把工作在编译器上。
#2
1
In Visual C++ there's a __countof()
construct that does the same. It's implemented as a template for C++ compiling and as a macro for C. The C++ version errors out if used on a pointer (as opposed to a true array), the C version does not.
在visualc++中,有一个__countof()构造也执行相同的操作。它被实现为c++编译的模板,作为C的宏,如果在指针上使用c++版本错误(与真正的数组相反),则C版本没有。
#3
1
I think what you're really asking is "Why does C++ insist on static typing?"
我认为您真正想问的是“为什么c++坚持使用静态类型?”
The answer: because it's easier to write a compiler that generates small, fast programs if the language uses static typing. And that's the purpose of C++: creating small, fast programs whose complexity would be relatively unmanageable if written in C.
答案是:因为如果语言使用静态类型,编写生成小型、快速程序的编译器会更容易。这就是c++的目的:创建小的、快速的程序,如果用C编写,其复杂性将相对难以管理。
When I say "small", I'm including the size of any required runtime libraries.
当我说“小”时,我包含了任何所需的运行时库的大小。