cygwin gcc 4.5.3
我正在尝试确定 C++ 中各种计算中使用的中间结果。对于'char',我得到以下结果(x 是一个字符):
x op y is of type char if y is a char/unsigned char
x op y is of type y otherwise except if op is left/right shift
对于左/右移位,中间结果是“int”类型,这与所有其他计算相反。这是标准实现吗?
这是使用的代码(不要太兴奋):
# include <iostream>
# include <iomanip>
# include <typeinfo>
# include "cstdio"
# include <cstdlib>
using namespace std;
typedef unsigned char UCHAR;
typedef unsigned short USHORT;
typedef unsigned int INT;
typedef unsigned long ULONG;
string find(type_info* info) {
struct type {
type_info* typeInfo;
string name;
};
int i;
static type types[] = { {const_cast<type_info*>(&typeid(bool)), " BOOL" }
, {const_cast<type_info*>(&typeid(char)), " CHAR" }
, {const_cast<type_info*>(&typeid(UCHAR)), " UCHAR" }
, {const_cast<type_info*>(&typeid(short)), " SHORT" }
, {const_cast<type_info*>(&typeid(USHORT)), " USHORT"}
, {const_cast<type_info*>(&typeid(int)), " INT" }
, {const_cast<type_info*>(&typeid(uint)), " UINT" }
, {const_cast<type_info*>(&typeid(long)), " LONG" }
, {const_cast<type_info*>(&typeid(ULONG)), " ULONG" }
};
for(i = 0; i < sizeof(types)/sizeof(types[0]); i++ )
if (types[i].typeInfo == info) break;
if (i >= sizeof(types)/sizeof(types[0])) return " NOT FOUND";
return types[i].name;
}
void func0(char x) {
ios_base::fmtflags ioFlags = cout.flags();
string info = find(const_cast<type_info*>(&typeid(x)));
char save = x;
cout << "func0(char " << (long)x << ')' << endl;
cout << "!x " << setw(12) << (!x) << " 0x" << setw(8) << setfill('0') << hex << (!x) << find(const_cast<type_info*>(&typeid(!x))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "~x " << setw(12) << (~x) << " 0x" << setw(8) << setfill('0') << hex << (~x) << find(const_cast<type_info*>(&typeid(~x))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "+x " << setw(12) << (+x) << " 0x" << setw(8) << setfill('0') << hex << (+x) << find(const_cast<type_info*>(&typeid(+x))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "-x " << setw(12) << (-x) << " 0x" << setw(8) << setfill('0') << hex << (-x) << find(const_cast<type_info*>(&typeid(-x))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "++x " << setw(12) << (long)(++x) << " 0x" << setw(8) << setfill('0') << hex << (++x) << find(const_cast<type_info*>(&typeid(++x))) << endl;
cout.flags(ioFlags); cout << setfill(' '); x = save;
cout << "--x " << setw(12) << (long)(--x) << " 0x" << setw(8) << setfill('0') << hex << (--x) << find(const_cast<type_info*>(&typeid(--x))) << endl;
cout.flags(ioFlags); cout << setfill(' '); x = save;
cout << "x++ " << setw(12) << (long)(x++) << " 0x" << setw(8) << setfill('0') << hex << (x++) << find(const_cast<type_info*>(&typeid(x++))) << endl;
cout.flags(ioFlags); cout << setfill(' '); x = save;
cout << "x-- " << setw(12) << (long)(x--) << " 0x" << setw(8) << setfill('0') << hex << (x--) << find(const_cast<type_info*>(&typeid(x--))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << endl;
}
void func1(char x, char y) {
ios_base::fmtflags ioFlags = cout.flags();
cout << "func1(char " << (signed long)x << ", char " << (unsigned long)y << ')' << endl;
cout << "x + y " << setw(12) << (x + y) << " 0x" << setw(8) << setfill('0') << hex << (x + y) << find(const_cast<type_info*>(&typeid(x + y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x - y " << setw(12) << (x - y) << " 0x" << setw(8) << setfill('0') << hex << (x - y) << find(const_cast<type_info*>(&typeid(x - y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x * y " << setw(12) << (x * y) << " 0x" << setw(8) << setfill('0') << hex << (x * y) << find(const_cast<type_info*>(&typeid(x * y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
if (y == 0) {
cout << "x / y " << x << " / 0" << endl;
} else {
cout << "x / y " << setw(12) << (x / y) << " 0x" << setw(8) << setfill('0') << hex << (x / y) << find(const_cast<type_info*>(&typeid(x / y))) << endl;
}
cout.flags(ioFlags); cout << setfill(' ');
cout << "x % y " << setw(12) << (x & y) << " 0x" << setw(8) << setfill('0') << hex << (x & y) << find(const_cast<type_info*>(&typeid(x & y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x << y " << setw(12) << (x << y) << " 0x" << setw(8) << setfill('0') << hex << (x << y) << find(const_cast<type_info*>(&typeid(x << y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x >> y " << setw(12) << (x >> y) << " 0x" << setw(8) << setfill('0') << hex << (x >> y) << find(const_cast<type_info*>(&typeid(x >> y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x & y " << setw(12) << (x & y) << " 0x" << setw(8) << setfill('0') << hex << (x & y) << find(const_cast<type_info*>(&typeid(x & y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x | y " << setw(12) << (x | y) << " 0x" << setw(8) << setfill('0') << hex << (x | y) << find(const_cast<type_info*>(&typeid(x | y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x ^ x " << setw(12) << (x ^ y) << " 0x" << setw(8) << setfill('0') << hex << (x ^ y) << find(const_cast<type_info*>(&typeid(x ^ y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << endl;
}
void func1(char x, unsigned char y) {
ios_base::fmtflags ioFlags = cout.flags();
cout << "func1(char " << (signed long)x << ", uchar " << (unsigned long)y << ')' << endl;
cout << "x + y " << setw(12) << (x + y) << " 0x" << setw(8) << setfill('0') << hex << (x + y) << find(const_cast<type_info*>(&typeid(x + y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x - y " << setw(12) << (x - y) << " 0x" << setw(8) << setfill('0') << hex << (x - y) << find(const_cast<type_info*>(&typeid(x - y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x * y " << setw(12) << (x * y) << " 0x" << setw(8) << setfill('0') << hex << (x * y) << find(const_cast<type_info*>(&typeid(x * y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
if (y == 0) {
cout << "x / y " << x << " / 0" << endl;
} else {
cout << "x / y " << setw(12) << (x / y) << " 0x" << setw(8) << setfill('0') << hex << (x / y) << find(const_cast<type_info*>(&typeid(x / y))) << endl;
}
cout.flags(ioFlags); cout << setfill(' ');
cout << "x % y " << setw(12) << (x & y) << " 0x" << setw(8) << setfill('0') << hex << (x & y) << find(const_cast<type_info*>(&typeid(x & y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x << y " << setw(12) << (x << y) << " 0x" << setw(8) << setfill('0') << hex << (x << y) << find(const_cast<type_info*>(&typeid(x << y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x >> y " << setw(12) << (x >> y) << " 0x" << setw(8) << setfill('0') << hex << (x >> y) << find(const_cast<type_info*>(&typeid(x >> y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x & y " << setw(12) << (x & y) << " 0x" << setw(8) << setfill('0') << hex << (x & y) << find(const_cast<type_info*>(&typeid(x & y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x | y " << setw(12) << (x | y) << " 0x" << setw(8) << setfill('0') << hex << (x | y) << find(const_cast<type_info*>(&typeid(x | y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x ^ x " << setw(12) << (x ^ y) << " 0x" << setw(8) << setfill('0') << hex << (x ^ y) << find(const_cast<type_info*>(&typeid(x ^ y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << endl;
}
void func1(char x, unsigned long y) {
ios_base::fmtflags ioFlags = cout.flags();
cout << "func1(char " << (signed long)x << ", ulong " << y << ')' << endl;
cout << "x + y " << setw(12) << (x + y) << " 0x" << setw(8) << setfill('0') << hex << (x + y) << find(const_cast<type_info*>(&typeid(x + y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x - y " << setw(12) << (x - y) << " 0x" << setw(8) << setfill('0') << hex << (x - y) << find(const_cast<type_info*>(&typeid(x - y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x * y " << setw(12) << (x * y) << " 0x" << setw(8) << setfill('0') << hex << (x * y) << find(const_cast<type_info*>(&typeid(x * y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
if (y == 0) {
cout << "x / y " << x << " / 0" << endl;
} else {
cout << "x / y " << setw(12) << (x / y) << " 0x" << setw(8) << setfill('0') << hex << (x / y) << find(const_cast<type_info*>(&typeid(x / y))) << endl;
}
cout.flags(ioFlags); cout << setfill(' ');
cout << "x % y " << setw(12) << (x & y) << " 0x" << setw(8) << setfill('0') << hex << (x & y) << find(const_cast<type_info*>(&typeid(x & y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x << y " << setw(12) << (x << y) << " 0x" << setw(8) << setfill('0') << hex << (x << y) << find(const_cast<type_info*>(&typeid(x << y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x >> y " << setw(12) << (x >> y) << " 0x" << setw(8) << setfill('0') << hex << (x >> y) << find(const_cast<type_info*>(&typeid(x >> y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x & y " << setw(12) << (x & y) << " 0x" << setw(8) << setfill('0') << hex << (x & y) << find(const_cast<type_info*>(&typeid(x & y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x | y " << setw(12) << (x | y) << " 0x" << setw(8) << setfill('0') << hex << (x | y) << find(const_cast<type_info*>(&typeid(x | y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x ^ x " << setw(12) << (x ^ y) << " 0x" << setw(8) << setfill('0') << hex << (x ^ y) << find(const_cast<type_info*>(&typeid(x ^ y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << endl;
}
void func1(char x, long y) {
ios_base::fmtflags ioFlags = cout.flags();
cout << "func1(char " << (signed long)x << ", long " << y << ')' << endl;
cout << "x + y " << setw(12) << (x + y) << " 0x" << setw(8) << setfill('0') << hex << (x + y) << find(const_cast<type_info*>(&typeid(x + y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x - y " << setw(12) << (x - y) << " 0x" << setw(8) << setfill('0') << hex << (x - y) << find(const_cast<type_info*>(&typeid(x - y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x * y " << setw(12) << (x * y) << " 0x" << setw(8) << setfill('0') << hex << (x * y) << find(const_cast<type_info*>(&typeid(x * y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
if (y == 0) {
cout << "x / y " << x << " / 0" << endl;
} else {
cout << "x / y " << setw(12) << (x / y) << " 0x" << setw(8) << setfill('0') << hex << (x / y) << find(const_cast<type_info*>(&typeid(x / y))) << endl;
}
cout.flags(ioFlags); cout << setfill(' ');
cout << "x % y " << setw(12) << (x & y) << " 0x" << setw(8) << setfill('0') << hex << (x & y) << find(const_cast<type_info*>(&typeid(x & y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x << y " << setw(12) << (x << y) << " 0x" << setw(8) << setfill('0') << hex << (x << y) << find(const_cast<type_info*>(&typeid(x << y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x >> y " << setw(12) << (x >> y) << " 0x" << setw(8) << setfill('0') << hex << (x >> y) << find(const_cast<type_info*>(&typeid(x >> y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x & y " << setw(12) << (x & y) << " 0x" << setw(8) << setfill('0') << hex << (x & y) << find(const_cast<type_info*>(&typeid(x & y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x | y " << setw(12) << (x | y) << " 0x" << setw(8) << setfill('0') << hex << (x | y) << find(const_cast<type_info*>(&typeid(x | y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << "x ^ x " << setw(12) << (x ^ y) << " 0x" << setw(8) << setfill('0') << hex << (x ^ y) << find(const_cast<type_info*>(&typeid(x ^ y))) << endl;
cout.flags(ioFlags); cout << setfill(' ');
cout << endl;
}
int main(int argc, char** argv) {
char x = -1;
func0((char) 1);
func1(x, (char) 1);
func1(x, (unsigned char) 1);
func1(x, (long) 1);
func1(x, (unsigned long) 1);
sleep(1);
return 0;
}