区分类型定义
Differentiate typedefs
我正在为 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>;
按照@MooingDuck的评论使用BOOST_STRONG_TYPEDEF。
- 使用QJsEngine在Qt中注册自定义类型
- 在UE4中使用未定义类型'UTextBlock'
- 修改"std::set"中用户定义类型的值
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 如何使自定义类型在unordered_map中用作键
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 为什么转换函数声明不需要至少一个定义类型说明符
- 标准::原子中的自定义类型
- 如何使用自定义类型声明Arduino数组?
- 如何在 Cython 中定义返回 cpp 定义类型的函数?
- 使用自定义访问者时具有自定义类型的提升变体失败(源自 boost::static_visitor)
- 您可以将binary_search应用于具有自定义类型的矢量吗?
- 在自定义类型图中重用 SWIG 映射
- 扩展自定义类型的spdlog
- vim使用户定义类型的COLOR与C++中的基本类型相同
- 重载自定义类型的 std::to_string 和 std::to_chars?
- 具有未声明/未定义类型的 typedef 结构
- 函数重载:内置类型与用户定义类型