由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - override operator[] inline
相关主题
一个C的void指针的问题问问C++的diamond problem
有些人练java走火入魔,急需吃药问个函数指针指向操作符的问题
基本功不扎实,问个问题一切没有括号来包裹逻辑块的语言都是纸老虎
*(&b1)=b编译不过,b1=b可以,区别是?Scala的operator似乎不太好读
C++ OO approach to use multi-dim array for HPCc的问题(2)
请问C++返回值和返回引用区别重载 ^ 操作符编译出错
为什么在overloading中,friend <<不能读取private值呢?请问c++中操作符可以声明为虚函数吗?
C++ class怎么定义double array啊能否对某个库进行操作符重载?
相关话题的讨论汇总
话题: inline话题: operator话题: index话题: array话题: require
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
能否对某个库进行操作符重载?C++ OO approach to use multi-dim array for HPC
A question about sharing data inside a C++ class请问C++返回值和返回引用区别
c++ private 问题为什么在overloading中,friend <<不能读取private值呢?
C++编程问题:union inside structC++ class怎么定义double array啊
一个C的void指针的问题问问C++的diamond problem
有些人练java走火入魔,急需吃药问个函数指针指向操作符的问题
基本功不扎实,问个问题一切没有括号来包裹逻辑块的语言都是纸老虎
*(&b1)=b编译不过,b1=b可以,区别是?Scala的operator似乎不太好读
相关话题的讨论汇总
话题: inline话题: operator话题: index话题: array话题: require