在C ++中使用typedef获得更多类型安全 [英] use typedef for more type safe in c++

查看:58
本文介绍了在C ++中使用typedef获得更多类型安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重新访问帖子.目前,我正在尝试避免在程序中混合使用Rad和Degree类型的角度而产生的错误.

I would like to revisit the post. Currently, I am trying to avoid bug created by mixing Rad and Degree types for angles in my program.

例如:

  typedef float Degree;
  typedef float Radian;

  Degree a = 15.;

  Radian b = 3.14/4.;

  float c = a + b;  // no compile error

是否有新的更新解决方案?

is there new update solution for this?

编辑01:

我希望编写自己的类,希望它的大小小并且没有依赖性.这是工作代码

I resort to write my own class with hope for its small size and no dependency. Here's the working code

#include <stdio.h>
#include <iostream>

template<typename numT>
class RadAngle {
public:
    RadAngle() {
      AngVal = 0.0;
    }
    RadAngle(const numT& angV) {
      AngVal = angV;
    }
    void operator = (const RadAngle<numT>& ang1) {
      AngVal = ang1.getVal();
    }
    RadAngle operator+(const RadAngle<numT>& ang1) const { return RadAngle<numT>(AngVal+ang1.getVal()); }
    RadAngle operator-(const RadAngle<numT>& ang1) const { return RadAngle<numT>(AngVal-ang1.getVal()); }
    RadAngle operator*(const RadAngle<numT>& ang1) const { return RadAngle<numT>(AngVal*ang1.getVal()); }
    RadAngle operator/(const RadAngle<numT>& ang1) const { return RadAngle<numT>(AngVal/ang1.getVal()); }

    numT getVal() const { return AngVal;};

private:
    numT AngVal;
};


int main() {

   RadAngle<float> a(1.5);
   RadAngle<float> b(3.14);
   RadAngle<float> c = a+b;

   //std::cout << c << std::endl;
//    printf("%.2f",c.getVal());


   return 0;
}

推荐答案

您所拥有的一切根本无助于键入安全性,除非是薄弱的文档.就编译器而言, float Degree Radian 是完整的同义词.这称为弱类型别名.强类型别名不是C ++的一部分,但是您可以解决此问题.关于这两篇好文章,请此处此处.基本思想是为强类型定义创建通用类模板,并使用该模板创建每个单独的别名.

What you have doesn't help type safety at all, except perhaps as weak documentation. As far as the compiler is concerned, float, Degree, and Radian are complete synonyms; this is called a weak type alias. Strong type aliases are not a part of C++, but you can work around that. Two good articles on that are here and here. The basic idea is to create a generic class template for a strong typedef, and use that to create each individual alias.

如果您甚至不想一次编写所有样板,我建议使用第三方库来处理.我上面链接的帖子的两位作者都为此编写了库, NamedType Boost.单位.请注意,我本人还没有使用过这些工具.它们只是我检查是否需要这些功能的地方.

If you don't want to write all the boilerplate yourself even once, I recommend using a third-party library to handle this. Both the authors of the posts I linked above wrote libraries for it, NamedType and type_safe. If you need something heavier-duty, you should check out Boost.Units. Note that I haven't used any of these myself; they're just where I'd check if I needed those features.

您没有问这个问题,但是,这不仅仅是在任何地方都使用 float 并手动跟踪单位,这都不会有任何运行时性能成本,但可能会使编译速度变慢.

You didn't ask about this, but none of this should have any runtime performance costs over just using float everywhere and keeping track of units manually, but might make compilation slower.

这篇关于在C ++中使用typedef获得更多类型安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆