区分类型定义

Differentiate typedefs

本文关键字:定义 类型      更新时间:2023-10-16

我正在为 C 库编写一个C++抽象。C 库具有多个用于标识远程资源的 ID 的类型定义:

typedef int color_id;
typedef int smell_id;
typedef int flavor_id;
// ...
color_id createColor( connection* );
void destroyColor( connection*, color_id );
// ...

所有这些 typedef 在编译器眼中当然是相同的类型。这对我来说是一个问题,因为我想重载函数并专门化模板以提供一个不错的C++友好的 API:

// can't do the following since `color_id`, `smell_id` and `int` are the same
std::ostream& operator<<( std::ostream&, color_id );
std::ostream& operator<<( std::ostream&, smell_id );
void destroy( connection*, color_id );
void destroy( connection*, smell_id );
// no static check can prevent the following
smell_id smell = createSmell( connection );
destroyColor( connection, smell ); // it's a smell, not a color!
  • 如何区分这些 ID,以利用类型安全并重载/专用化函数和类?

由于我不知道任何其他方法,我一直在考虑为每个 C 类型创建一个不同的包装器类型。但这条路似乎相当崎岖...

  1. 已经有很多代码专门用于基元类型(例如std::hash)。
    有没有办法告诉编译器"如果某物对int有专门的,但对我的包装器没有,那么就使用int的特化"?
    否则我应该为像std::hash这样的东西写专业化吗?那些不在std中的类似模板化结构(例如 boost、Qt 等)呢?

  2. 我应该使用隐式还是显式构造函数和强制转换运算符?显式的当然更安全,但它们会使与使用 C API 的现有代码和第三方库进行交互变得非常繁琐。

我非常愿意接受已经去过那里的人的任何提示!

一个包装类来统治它们

最好的办法是创建一个包装类,但是使用模板,我们可以编写一个包装类模板,并通过将它们分配给模板的不同实例来将其用于所有不同的ID。

template<class ID> 
struct ID_wrapper
{
constexpr static auto name() -> decltype(ID::name()) {
return ID::name(); 
}
int value;
// Implicitly convertible to `int`, for C operability
operator int() const {
return value; 
}  
};

重载std::hash(仅一次)

我们可以在ID类中保留我们想要的任何特征,但我提供了name()作为示例。由于ID_Wrapper是作为模板编写的,因此只需将其专门用于std::hash和其他类只需执行一次:

template<class ID>
class std::hash<ID_wrapper<ID>> : public std::hash<int>
{
public:
// I prefer using Base to typing out the actual base
using Base = std::hash<int>;
// Provide argument_type and result_type
using argument_type = int;
using result_type = std::size_t; 
// Use the base class's constructor and function call operator
using Base::Base; 
using Base::operator(); 
};

打印出带有其名称的 ID

如果你愿意,我们也可以专门化operator<<,但无论如何ID_wrapper都可以隐式转换为int

template<class ID>
std::ostream& operator<<(std::ostream& stream, ID_Wrapper<ID> id) {
stream << '(' << ID_Wrapper<ID>::name() << ": " << id.value << ')'; 
return stream; 
}

一旦我们有了它,我们只需为每个 ID 类型编写一个特征类!

struct ColorIDTraits {
constexpr static const char* name() {
return "color_id";
}
};
struct SmellIDTraits {
constexpr static const char* name() {
return "smell_id";
}
};
struct FlavorIDTraits {
constexpr static const char* name() {
return "flavor_id";
}
};

将它们全部包装在一起

然后我们可以typedefID_wrapper:

using color_id = ID_wrapper<ColorIDTraits>;
using smell_id = ID_wrapper<SmellIDTraits>;
using flavor_id = ID_wrapper<FlavorIDTraits>;

按照@MooingDuck的评论使用BOOST_STRONG_TYPEDEF。