我正在为 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 类型创建不同的包装器类型。但是这条路似乎很坎坷……
已经有很多专门用于基本类型的代码(例如
std::hash
)。
有什么方法可以告诉编译器类似“如果某些东西具有int
的特化,但不是我的包装器,那么只需使用int
特化”?
我是否应该为std::hash
之类的东西编写专门化?std
中没有的类似模板化结构(例如 boost、Qt 等中的内容)怎么办?我应该使用隐式构造函数还是显式构造函数和转换运算符?显式的当然更安全,但它们会使与现有代码和使用 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";
}
};
将它们包装在一起
然后我们可以typedef
ID_wrapper:
using color_id = ID_wrapper<ColorIDTraits>;
using smell_id = ID_wrapper<SmellIDTraits>;
using flavor_id = ID_wrapper<FlavorIDTraits>;
关于c++ - 区分 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55754107/