区分typedef

peo*_*oro 5 c++ types typedef overloading

我正在为C库编写C ++抽象。C库具有用于标识远程资源的ID的几种typedef:

typedef int color_id;
typedef int smell_id;
typedef int flavor_id;
// ...

color_id createColor( connection* );
void destroyColor( connection*, color_id );
// ...
Run Code Online (Sandbox Code Playgroud)

在编译器看来,所有这些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!
Run Code Online (Sandbox Code Playgroud)
  • 我如何区分这些ID,以利用类型安全性和重载/专业化功能和类的优势?

由于我不知道其他任何方式,因此我一直在考虑为每种C类型创建不同的包装器类型。但是这条路似乎很艰难...

  1. 有很多代码已经专门用于原始类型(例如std::hash)。
    有什么方法可以告诉编译器,例如“如果某项具有的专门化int,但没有针对我的包装器,则只需使用int专门化”?
    我是否应该为诸如此类的东西写专长std::hash?那些不在其中的类似模板结构如何处理std(例如boost,Qt等)?

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

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

J. *_*rez 3

一个包装类来统治它们

最好的选择是创建一个包装类,但是使用模板,我们可以编写一个包装类模板,并将其用于所有不同的 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; 
    }  
};
Run Code Online (Sandbox Code Playgroud)

超载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(); 
};
Run Code Online (Sandbox Code Playgroud)

打印出带有姓名的 ID

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

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

一旦我们有了这个,我们只需为每个 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";
    }
};
Run Code Online (Sandbox Code Playgroud)

将它们全部包裹在一起

然后我们可以使用typedefID_wrapper:

using color_id = ID_wrapper<ColorIDTraits>;
using smell_id = ID_wrapper<SmellIDTraits>;
using flavor_id = ID_wrapper<FlavorIDTraits>;
Run Code Online (Sandbox Code Playgroud)