宏的重写以遵循 C++ 约定
macro's rewrite to follow the c++ convention
如何重写下面的宏,使它们真正遵循c++约定?(在C++中,我们更喜欢使用typedef
、const
和inline
函数)
这是宏
#define UBYTE unsigned char
#define ULONG unsigned long
#define FALSE 0
#define COMPRESS_MAX_COM 0x70000000
#define COMPRESS_MAX_ORG (1024-COMPRESS_OVERRUN)
#define MAX_RAW_GROUP (16*MAX_RAW_ITEM)
#define U(X) ((ULONG) X)
如何将上面的宏更改为c++约定?
对类型使用typedef
或using
(C++11)而不是#define
,然后对常量使用const
。在C++11中,这些函数可以定义为constexpr
。
typedef unsigned char UBYTE; // using UBYTE = unsigned char; // C++11
typedef unsigned long ULONG;
const unsigned int FALSE=0; // constexpr usigned int FALSE = 0; // C++11
const ULONG COMPRESS_MAX_COM=0x70000000;
const ULONG COMPRESS_MAX_ORG=(1024-COMPRESS_OVERRUN); // if COMPRESS_OVERRUN is const
const ULONG MAX_RAW_GROUP=(16*MAX_RAW_ITEM); // again if MAX_RAW_ITEM is a previously defined const
#define U(X) ((ULONG) X) // this can stay as it is
或
template <typename T>
ULONG U(const T& x)
{
return static_cast<ULONG>(x);
}
所以CCD_ 9将返回其类型为CCD_。
您应该考虑整数大小,尤其是在接口中使用这些定义的情况下。
例如:
#include <stdint.h>
typedef uint8_t UBYTE; // using UBYTE = unsigned char; // C++11
typedef uint32_t ULONG;
const uint32_t FALSE=0; // constexpr unsigned int FALSE = 0; // C++11
vsoftco上面给出的其他定义都很棒:
const ULONG COMPRESS_MAX_COM=0x70000000;
const ULONG COMPRESS_MAX_ORG=(1024-COMPRESS_OVERRUN); // if COMPRESS_OVERRUN is const
const ULONG MAX_RAW_GROUP=(16*MAX_RAW_ITEM); // again if MAX_RAW_ITEM is a previously defined const
#define U(X) ((ULONG) X) // this can stay as it is
相关文章:
- "std::unique_XXX"命名约定背后的基本原理是什么?
- 为什么在 x64 中忽略__stdcall调用约定?
- 引发异常的方法的命名约定 (C++)?
- 窗口调用约定
- 我应该提到纯虚函数中的调用约定吗?
- C++:在进行切片时对迭代器的约定,特别是对于访问最后一个元素并最终将其删除
- 如何使用gcc指定stdcall调用约定
- Clang:x86 FPU调用约定
- 该标准是否说明了例外和不同调用约定的共存
- Clang++的InterlockedExchange的32位调用约定错误,但MSVC可以
- std::sort & comp - 调用约定?
- C++命名空间约定
- C++的新手试图弄清楚类约定
- 如何为类 /块 /范围选择C 调用约定
- C 未知呼叫约定
- C++关于传递对象(指针与引用)的约定
- DLL 的"good"调用约定是什么?
- 结构中重载运算符的调用约定
- 一些编码约定/标准实践问题
- 'ShowSUM':__declspec(dllexport)不能应用于具有__clrcall调用约定的函数