我尝试考虑一种 API 策略,在头文件中,而不是
typedef struct {...} type_t;
extern void func(const type_t* instance);
我有
typedef void* type_t_handle;
extern void func(const type_t_handle instance);
但我现在发现,虽然之前我有选择
const type_t* const x; // (1)
const type_t* x; // (2)
type_t* const x; // (3)
我现在只有选项(3)(当我看到编写
const type_t_handle const x;
会导致“重复的‘const’声明说明符”警告时,我进行了检查)。换句话说,typedef
似乎“绑定”了*
和基本类型,我们不能再在两者之间“挤压”const...
这意味着参数
const
中的 const type_t_handle instance
是无用的,我认为 - 它不会导致“传递参数...丢弃'const'限定符”类型的警告,这将提醒人们不修改的意图...
有什么解决办法吗?
您是正确的,
typedef
将指针绑定到基本类型,因此您无法创建指向类型const
。
您需要为
typedef
合格版本制作单独的 const
。
typedef void* type_t_handle;
typedef const void* const_type_t_handle;