C++ 使用和typedef的强类型

C++ 使用和typedef的强类型,c++,c++14,c++17,C++,C++14,C++17,在我们的项目中,我们使用了相当多的“using”来明确说明变量应该表示什么。它主要用于std::string标识符,如PortalId或CakeId。现在我们能做的是 using PortalId = std::string; using CakeId = std::string; PortalId portal_id("2"); CakeId cake_id("is a lie"); portal_id = cake_id; // OK 这是我们不喜欢的。我们希望在编译时进行类型检查

在我们的项目中,我们使用了相当多的“using”来明确说明变量应该表示什么。它主要用于
std::string
标识符,如
PortalId
CakeId
。现在我们能做的是

using PortalId = std::string;
using CakeId   = std::string;

PortalId portal_id("2");
CakeId cake_id("is a lie");

portal_id = cake_id; // OK
这是我们不喜欢的。我们希望在编译时进行类型检查,以防止在保留原始对象中的大多数yum-yum方法的同时混用apple和oranges

问题是-这可以在C++中完成,这样使用将接近下面的内容,赋值会失败,我们仍然可以使用它,比如地图和其他容器?

SAFE_TYPEDEF(std::string, PortalId);
SAFE_TYPEDEF(std::string, CakeId);

int main()
{
    PortalId portal_id("2");
    CakeId cake_id("is a lie");
    std::map<CakeId, PortalId> p_to_cake; // OK

    p_to_cake[cake_id]   = portal_id; // OK
    p_to_cake[portal_id] = cake_id;   // COMPILER ERROR

    portal_id = cake_id;        // COMPILER ERROR
    portal_id = "1.0";          // COMPILER ERROR
    portal_id = PortalId("42"); // OK
    return 0;

}
SAFE_TYPEDEF(std::string,PortalId);
SAFE_TYPEDEF(std::string,CakeId);
int main()
{
PortalId portal_id(“2”);
CakeId cake_id(“是谎言”);
std::将p_映射到_蛋糕;//确定
p_to_cake[cake_id]=portal_id;//确定
p_to_cake[portal_id]=cake_id;//编译器错误
portal\u id=cake\u id;//编译器错误
portal_id=“1.0”//编译器错误
portal_id=PortalId(“42”);//确定
返回0;
}
我们已经尝试过将宏与模板结合使用,但并没有得到我们所需要的。另外,我们可以使用c++17

编辑:我们提出的代码是

#define SAFE_TYPEDEF(Base, name) \
class name : public Base { \
public: \
    template <class... Args> \
    explicit name (Args... args) : Base(args...) {} \
    const Base& raw() const { return *this; } \
};
#定义安全类型定义(基础、名称)\
类名:公共基{\
公众:\
模板\
显式名称(Args…Args):基(Args…{}\
常量Base&raw()常量{return*this;}\
};
这很难看而且不起作用。我的意思是,编译器可以使用
portal\u id=cake\u id


EDIT2:添加了
explicit
关键字,对于我们的示例,我们的代码实际上可以很好地使用该关键字。不确定这是否是正确的方法,也不确定它是否涵盖了所有不幸的情况。

如果有一个标准的方法可以做到这一点,那就好了,但目前还没有。将来可能会有一些标准化的东西:有一篇论文试图用函数别名和更丰富的继承结构来实现这一点,还有一篇论文采用了更简单的方法,使用一个新的关键字来引入强typedef,或者任何你想称之为强typedef的东西

Boost序列化库提供了可能会满足您需要的功能

这里有一个替换您的
SAFE_TYPEDEF
,它只是
BOOST\u STRONG_TYPEDEF
,没有其他BOOST依赖项,并且经过轻微修改,因此您无法从
TYPEDEF
d类型进行分配。我还添加了移动构造函数和赋值运算符,并使用了
default

namespace detail {
    template <typename T> class empty_base {};
}

template <class T, class U, class B = ::detail::empty_base<T> >
struct less_than_comparable2 : B
{
     friend bool operator<=(const T& x, const U& y) { return !(x > y); }
     friend bool operator>=(const T& x, const U& y) { return !(x < y); }
     friend bool operator>(const U& x, const T& y)  { return y < x; }
     friend bool operator<(const U& x, const T& y)  { return y > x; }
     friend bool operator<=(const U& x, const T& y) { return !(y < x); }
     friend bool operator>=(const U& x, const T& y) { return !(y > x); }
};

template <class T, class B = ::detail::empty_base<T> >
struct less_than_comparable1 : B
{
     friend bool operator>(const T& x, const T& y)  { return y < x; }
     friend bool operator<=(const T& x, const T& y) { return !(y < x); }
     friend bool operator>=(const T& x, const T& y) { return !(x < y); }
};

template <class T, class U, class B = ::detail::empty_base<T> >
struct equality_comparable2 : B
{
     friend bool operator==(const U& y, const T& x) { return x == y; }
     friend bool operator!=(const U& y, const T& x) { return !(x == y); }
     friend bool operator!=(const T& y, const U& x) { return !(y == x); }
};

template <class T, class B = ::detail::empty_base<T> >
struct equality_comparable1 : B
{
     friend bool operator!=(const T& x, const T& y) { return !(x == y); }
};

template <class T, class U, class B = ::detail::empty_base<T> >
struct totally_ordered2
    : less_than_comparable2<T, U
    , equality_comparable2<T, U, B
      > > {};

template <class T, class B = ::detail::empty_base<T> >
struct totally_ordered1
    : less_than_comparable1<T
    , equality_comparable1<T, B
      > > {};

#define SAFE_TYPEDEF(T, D)                                      \
struct D                                                        \
    : totally_ordered1< D                                       \
    , totally_ordered2< D, T                                    \
    > >                                                         \
{                                                               \
    T t;                                                        \
    explicit D(const T& t_) : t(t_) {};                         \
    explicit D(T&& t_) : t(std::move(t_)) {};                   \ 
    D() = default;                                              \
    D(const D & t_) = default;                                  \
    D(D&&) = default;                                           \
    D & operator=(const D & rhs) = default;                     \
    D & operator=(D&&) = default;                               \
    operator T & () { return t; }                               \
    bool operator==(const D & rhs) const { return t == rhs.t; } \
    bool operator<(const D & rhs) const { return t < rhs.t; }   \
};
名称空间详细信息{
模板类空_base{};
}
模板
结构小于可比2:B
{
友元布尔算子y);}
友元布尔运算符>=(常数T&x,常数U&y){return!(x(常数U&x,常数T&y){返回yx);}
};
模板
结构小于可比1:B
{
友元布尔运算符>(常数T&x,常数T&y){返回y >                                                         \
{                                                               \
T\
显式D(const T&T):T(T{}\
显式D(T&&T):T(std::move(T){};\
D()=默认值\
D(常数D&t)=默认值\
D(D&&)=默认值\
D和运算符=(常量D和rhs)=默认值\
D&运算符=(D&&)=默认值\
运算符T&({return T;}\
布尔运算符==(常量D&rhs)常量{return t==rhs.t;}\

bool操作符这里有一个最小的完整解决方案,可以满足您的要求

您可以添加更多运算符等,使该类在您认为合适的情况下更有用

#include <iostream>
#include <string>
#include <map>

// define some tags to create uniqueness 
struct portal_tag {};
struct cake_tag {};

// a string-like identifier that is typed on a tag type   
template<class Tag>
struct string_id
{
    // needs to be default-constuctable because of use in map[] below
    string_id(std::string s) : _value(std::move(s)) {}
    string_id() : _value() {}

    // provide access to the underlying string value        
    const std::string& value() const { return _value; }
private:
    std::string _value;

    // will only compare against same type of id.
    friend bool operator < (const string_id& l, const string_id& r) {
        return l._value < r._value;
    }
};


// create some type aliases for ease of use    
using PortalId = string_id<portal_tag>;
using CakeId = string_id<cake_tag>;

using namespace std;

// confirm that requirements are met
auto main() -> int
{
    PortalId portal_id("2");
    CakeId cake_id("is a lie");
    std::map<CakeId, PortalId> p_to_cake; // OK

    p_to_cake[cake_id]   = portal_id; // OK
//    p_to_cake[portal_id] = cake_id;   // COMPILER ERROR

//    portal_id = cake_id;        // COMPILER ERROR
//    portal_id = "1.0";          // COMPILER ERROR
    portal_id = PortalId("42"); // OK
    return 0;
}

到目前为止,提供的解决方案似乎过于复杂,因此我尝试如下:

#include <string>

enum string_id {PORTAL, CAKE};

template <int ID> class safe_str : public std::string {
    public:
    using std::string::string;
};

using PortalId = safe_str<PORTAL>;
using CakeId = safe_str<CAKE>;
#包括
枚举字符串_id{PORTAL,CAKE};
模板类safe_str:public std::string{
公众:
使用std::string::string;
};
使用PortalId=safe_str;
使用CakeId=safe_str;

最近,我遇到了一个名为的库,它提供了包装精美的语法糖,可以完全满足我们的需要!使用该库,我们的示例如下所示:

namespace fl = fluent;
using PortalId = fl::NamedType<std::string, struct PortalIdTag>;
using CakeId = fl::NamedType<std::string, struct CakeIdTag, fl::Comparable>;

int main()
{
    PortalId portal_id("2");
    CakeId cake_id("is a lie");
    std::map<CakeId, PortalId> p_to_cake; // OK

    p_to_cake.emplace(cake_id, portal_id); // OK
    // p_to_cake.emplace(portal_id, cake_id);  // COMPILER ERROR

    // portal_id = cake_id;        // COMPILER ERROR
    // portal_id = "1.0";          // COMPILER ERROR
    portal_id = PortalId("42"); // OK
    return 0;
}
名称空间fl=fluent;
使用PortalId=fl::NamedType;
使用CakeId=fl::NamedType;
int main()
{
PortalId portal_id(“2”);
CakeId cake_id(“是谎言”);
std::将p_映射到_蛋糕;//确定
p_to_cake.emplace(cake_id,portal_id);//确定
//p_to_cake.emplace(portal_id,cake_id);//编译器错误
//portal\u id=cake\u id;//编译器错误
//portal_id=“1.0”//编译器错误
portal_id=PortalId(“42”);//确定
返回0;
}
NamedTypes
library提供了更多的
namespace fl = fluent;
using PortalId = fl::NamedType<std::string, struct PortalIdTag>;
using CakeId = fl::NamedType<std::string, struct CakeIdTag, fl::Comparable>;

int main()
{
    PortalId portal_id("2");
    CakeId cake_id("is a lie");
    std::map<CakeId, PortalId> p_to_cake; // OK

    p_to_cake.emplace(cake_id, portal_id); // OK
    // p_to_cake.emplace(portal_id, cake_id);  // COMPILER ERROR

    // portal_id = cake_id;        // COMPILER ERROR
    // portal_id = "1.0";          // COMPILER ERROR
    portal_id = PortalId("42"); // OK
    return 0;
}