根据编译时常量,使用相同的标识符 #define 或类型定义是否被认为是可接受的做法?

Is it considered acceptable practice to have the same identifier #define'd or typedef'ed, depending on compile-time constants?

本文关键字:是否 定义 类型 认为是 可接受 #define 标识符 常量 编译      更新时间:2023-10-16

"英特尔数学内核库"在头文件中包含以下代码:

#ifndef MKL_Complex16
typedef
struct _MKL_Complex16 {
    double real;
    double imag;
} MKL_Complex16;
#endif

正如这里所描述和讨论的,用户可以通过编写来覆盖这个结构定义

#define MKL_Complex16 std::complex<double>

在包括头文件之前。对于这一行,MKL_Complex16#define'd,这意味着字符串只是到处替换为文字字符std::complex<double>。如果不是,则为typedef’ed,这将为编译器提供更多信息。

这被认为是可以接受的做法吗?我想它一定是,因为它是由英特尔实现的。但在调试某些代码时,我发现它非常令人困惑。

这看起来很疯狂。我会非常不愿意在构建环境中传递实际的代码。如果有什么不同的话,请使用更高级别的标志:

#ifndef HAVE_STD_COMPLEX
    struct MKL_Complex16 { double real; double img; };
#else
#  include <complex>
   typedef std::complex<double> MKL_Complex16;
#endif