编译器强制语义类型

Compiler-enforced semantic types

假设我有一个 class 表示自动机,其状态已编号 (using state_t = unsigned),其过渡子也已编号 (using transition_t = unsigned)。当然在某些时候我最终搞砸了一些调用,因为 transition_tstate_t 是相同的类型,所以编译器不强制执行(语义)类型安全。使用由标签 (struct transition_tag {}; struct state_tag {};) 模板化的小 class 很容易解决这个问题,所以现在 transition_tstate_t 不兼容,很好!

/// Lightweight state/transition handle (or index).
template <typename Tag>
struct index_t_impl
{
  using index_t = unsigned;

  constexpr index_t_impl(index_t i)
    : s{i}
  {}

  // Disallow index1_t i{index2_t{42}};
  template <typename T>
  index_t_impl(index_t_impl<T> t) = delete;

  bool operator==(index_t_impl t) const
  {
    return s == t.s;
  }

  // Disallow index1_t{42} ==  index2_t{42};
  template <typename T>
  bool operator==(index_t_impl<T> t) const = delete;

  /// Default ctor to please containers.
  index_t_impl() = default;
  constexpr operator index_t() const { return s; }
  /// Be compliant with Boost integer ranges.
  index_t_impl& operator++() { ++s; return *this; }
  /// Be compliant with Boost integer ranges.
  index_t_impl& operator--() { --s; return *this; }

private:
  index_t s;
};

此外,我有两个非常相似的结构:

然后我又遇到了这个问题,我将 std::vector<transition_t> 用于两个完全不同的目的。当然,我可以再次引入一个由标签模板化的包装器,但事情又变得一团糟了。 Public继承很诱人(Thou shalt not inherit from std::vector)!

但实际上,每次我想引入与基本类型完全相似但只是不兼容的新类型时,我都厌倦了临时解决方案。在这方面有什么建议吗? public 继承确实很吸引人,但它不会引入大量额外实例化的代码膨胀吗?也许 Crashworks () 推荐的 public 组合 (struct predecessors_t { std::vector<transition_t> v; };) 是一个更好的选择,可以更好地扩展?

C++ 的未来是否有什么可以解决这个新问题的?

这个获取编译器强制语义类型的问题可能会在各种情况下出现,从你的情况到不同来源的坐标系统(其中值都是相同的类型(例如 int),但是从语义上讲,类型不能混合,因为它们表示来自不同来源的偏移量 (x,y,z=0,0,0)——这在数学中经常发生,其中,当用正 x 和 y 绘制象限时,原点在左下角,而计算机科学中通常将原点放在左上角)到宇宙飞船导航(更多信息见下文)。

2012 年,Bjarne Stroustrup 就他所谓的类型丰富编程 介绍了使用模板、用户定义的 C++11 编译器强制语义类型安全性的有趣演讲文字,声称 没有 运行 时间开销 实施,甚至还有从火星气候观察者 snafu 中吸取教训的故事(3.5 亿美元的航天器 + 由于缺乏强制语义类型而导致任务丢失)安全)。您可以在此处查看他介绍语义类型的演讲部分:https://youtu.be/0iWb_qi2-uI?t=19m6s

我已经根据 Stroustrup 的演示代码编写了示例代码摘录,更新到当前标准并实现了所需的运算符重载)。与 Bjarne 的示例不同,这个示例实际上可以编译。 ;)

这段代码的要点可以在这里找到:https://gist.github.com/u-007d/361221df5f8c7f3466f0f09dc96fb1ba

//Compiled with clang -std=c++14 -Weverything -Wno-c++98-compat main.cpp -o main

#include <iostream>
#include <string>

template<int M, int K, int S>  //Meters, Kilograms, Seconds (MKS)
struct Unit
{
    enum { m=M, kg=K, s=S };
};

template<typename Unit> //a magnitude with a unit
struct Value
{
    double val; //the magnitude
    constexpr explicit Value(double d) : val(d) {} //construct a Value from a double
};

//Basic Semantic Units for MKS domain
using Meter = Unit<1, 0, 0>;
using Kilogram = Unit<0, 1, 0>;
using Second = Unit<0, 0, 1>;
using Second2 = Unit<0, 0, 2>;

//Semantic Value Types for MKS domain
using Time = Value<Second>;
using Distance = Value<Meter>;
using Mass = Value<Kilogram>;
using Speed = Value<Unit<1, 0, -1>>; //Speed is meters/second
using Acceleration = Value<Unit<1, 0, -2>>; //Acceleration is meters/second^2

//Operator overloads to properly calculate units (incomplete; for demo purposes)
Speed operator/(const Distance& lhs, const Time& rhs)
{
    return Speed(lhs.val / rhs.val);
}

Acceleration operator/(const Speed& lhs, const Time& rhs)
{
    return Acceleration(lhs.val / rhs.val);
}

//Define literals
constexpr Distance operator"" _m(long double ld)
{
    return Distance(static_cast<double>(ld));
}

constexpr Mass operator"" _kg(long double ld)
{
    return Mass(static_cast<double>(ld));
}

constexpr Time operator"" _s(long double ld)
{
    return Time(static_cast<double>(ld));
}

constexpr Acceleration operator"" _s2(long double ld)
{
    return Acceleration(static_cast<double>(ld));
}

int main()
{
    Speed sp = Distance(100)/Time(9.58); //Not bad, but units could be more convenient...
    Distance d1 = 100.0_m; //A good distance to run a race
    Speed sp1 = 100.0_m/9.58_s; //A human can run this fast
//    Speed sp2 = 100.0_m/9.8_s2; //Error: speed is m/s, not m/s^2
//    Speed sp3 = 100.0/9.8_s; //Error: 100 has no unit
    Acceleration ac1 = sp1/0.5_s; //Faster than any human

    return EXIT_SUCCESS;
}