如何说服编译器优化运算符[]使其不跳转
struct Pixel
{
char r,g,b;
char & operator[](int idx){
swith(idx){
case 0: return r;
case 1: return g;
case 2: return b;
}
return *(&r+idx);
}
};
//奖励:constexpr,msvc
另外有一种方式:
struct Pixel
{
char colors[3];
char& r = colors[0];
char& g = colors[1];
char& b = colors[2];
char &operator[](int i);
};
char & Piex::operator[](int i)
{
return colors[i];
}
//这个会使得sizeof(Piexl) = 4*sizeof(void*);