【发布时间】:2012-01-20 11:50:52
【问题描述】:
#define MAX 265
std::cout << 0 * MAX << std::endl; //to my surprise, the output is 9 rather than 0
这个 C++ 宏乘法有什么问题?
编辑:
以下是完整版。
#include <stdio.h>
#include <string.h>
#include <iostream>
#define NAME_BYTES 256
#define VERSION_BYTES 256
#define SIZE_BYTES 32
#define USED_LOCK_COUNT_BYTES 32
#define LOCK_NAME_BYTES 256
#define LOCK_TYPE_BYTES 1
#define PID_BYTES 4
#define TID_BYTES 4
#define LOCK_BYTES LOCK_NAME_BYTES + LOCK_TYPE_BYTES + PID_BYTES + TID_BYTES
#define HEADER_BYTES NAME_BYTES + VERSION_BYTES + SIZE_BYTES + USED_LOCK_COUNT_BYTES
int main() {
std::cout << "LOCK_BYTES: " << LOCK_BYTES << std::endl;
std::cout << "HEADER_BYTES: " << HEADER_BYTES << std::endl;
std::cout << "LOCK_BYTES * 0: " << 0 * LOCK_BYTES << std::endl;
}
这是我刚刚得到的结果和编译器信息。
yifeng@yifeng-Precision-WorkStation-T3400:~/Shared-Memory-Solution/examples/IMPL$ g++ -v 使用内置规范。 COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper 目标:x86_64-linux-gnu 配置:../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++, fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr /lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with -sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch- 32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu 线程模型:posix gcc版本4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)
yifeng@yifeng-Precision-WorkStation-T3400:~/Shared-Memory-Solution/examples/IMPL$ ./a.out LOCK_BYTES: 265 HEADER_BYTES: 576 LOCK_BYTES * 0: 9
编辑:非常感谢你们!!我很高兴我决定发布这个,尽管我得到了很多反对票。学习 MACRO 是多么重要的一课!
【问题讨论】:
标签: c++ macros multiplication