i**p 发帖数: 902 | 1 Someone is asking this question in stackoverflow.com, and no good answer
there. I have the same question. Could anyone here give a best answer?
http://stackoverflow.com/questions/15867707/thinking-in-c-inlin
The following code is from Thinking in C++. The author mentioned that "Since
operator[] is an inline, you could use this approach to guarantee that no
array-bounds violations occur, then remove the require() for the shipping
code." What feature of inline function is referred here? Thanks!
#include "../require.h"
#include
using namespace std;
template
class Array {
enum { size = 100 };
T A[size];
public:
T& operator[](int index) {
require(index >= 0 && index < size,
"Index out of range");
return A[index];
}
}; |
p***o 发帖数: 1252 | 2 Array::operator[] will incur no performance penalty in comparison to
[] for built-in (C) arrays after 'require' is removed.
Since
【在 i**p 的大作中提到】 : Someone is asking this question in stackoverflow.com, and no good answer : there. I have the same question. Could anyone here give a best answer? : http://stackoverflow.com/questions/15867707/thinking-in-c-inlin : The following code is from Thinking in C++. The author mentioned that "Since : operator[] is an inline, you could use this approach to guarantee that no : array-bounds violations occur, then remove the require() for the shipping : code." What feature of inline function is referred here? Thanks! : #include "../require.h" : #include : using namespace std;
|
d****i 发帖数: 4809 | 3 这个不对吧,C里面没有操作符重载一说,数组的[]就是简单的指针操作,但是上述C++
里面的inline的操作符重载[]是一次函数调用,就会有点overhead,所以即便把中间的
require()去掉,还是比C稍有点performance hit。这个比较一下C和上面的C++生成的
机器码就知道了。
【在 p***o 的大作中提到】 : Array::operator[] will incur no performance penalty in comparison to : [] for built-in (C) arrays after 'require' is removed. : : Since
|
p***o 发帖数: 1252 | 4 We are talking about what is 'inline'.
Have you really checked the ASM list? If you see any extra operations,
say for std::vector::operator[], make sure you have read the documents
to turn off all debug helps/assertions.
++
【在 d****i 的大作中提到】 : 这个不对吧,C里面没有操作符重载一说,数组的[]就是简单的指针操作,但是上述C++ : 里面的inline的操作符重载[]是一次函数调用,就会有点overhead,所以即便把中间的 : require()去掉,还是比C稍有点performance hit。这个比较一下C和上面的C++生成的 : 机器码就知道了。
|
N******K 发帖数: 10202 | 5 我看过机器码 operator[] 这个函数优化之后 和 直接写 Array[index] 一样
++
【在 d****i 的大作中提到】 : 这个不对吧,C里面没有操作符重载一说,数组的[]就是简单的指针操作,但是上述C++ : 里面的inline的操作符重载[]是一次函数调用,就会有点overhead,所以即便把中间的 : require()去掉,还是比C稍有点performance hit。这个比较一下C和上面的C++生成的 : 机器码就知道了。
|
i**p 发帖数: 902 | 6 "Since operator[] is an inline, you could use this approach to guarantee
that no array-bounds violations occur, then remove the require() for the
shipping code."
这句话先强调了"Since operator[] is an inline", 而后半句说的是用require()保证
没有array-bounds violations occur. 难道不是inline的话就不能用require()了吗?
【在 p***o 的大作中提到】 : Array::operator[] will incur no performance penalty in comparison to : [] for built-in (C) arrays after 'require' is removed. : : Since
|