区分 typedef

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!

因为我不知道任何其他方式,所以我一直在考虑为每个 C 类型创建不同的包装器类型。但是这条路似乎很坎坷...

  1. 已经有很多代码专门用于基本类型(例如 std::hash)。
    有没有办法告诉编译器类似 "if something has a specialization for int, but not for my wrapper, then just use the int specialization"?
    我是否应该为 std::hash 之类的内容编写专业化? std 中没有的类似模板化结构(例如 boost、Qt 等中的内容)怎么办?

  2. 我应该使用隐式还是显式构造函数和转换运算符?明确的当然更安全,但它们会使与使用 C API.

  3. 的现有代码和第三方库交互变得非常乏味

我乐于接受任何去过那里的人的任何提示!

一个包装器 class 来统治它们

你最好的选择是创建一个包装器 class,但是使用模板我们可以编写一个包装器 class 模板并将它用于所有不同的 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 class 中添加我们想要的任何特征,但我提供了 name() 作为示例。由于 ID_Wrapper 是作为模板编写的,因此只需要为 std::hash 和其他 class 专门化它一次:

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 类型写一个特征 class!

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