Limiting range of value types in C++

前端 未结 9 815
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 10:33

Suppose I have a LimitedValue class which holds a value, and is parameterized on int types \'min\' and \'max\'. You\'d use it as a container for holding values which can on

9条回答
  •  鱼传尺愫
    2020-11-28 11:08

    This is actually a complex matter and I have tackled it for a while...

    Now I have a publicly available library that will allow you to limit floating points and integers in your code so you can make more sure that they are valid at all time.

    Not only that you can turn off the limits in your final release version and that means the types pretty much become the same as a typedef.

    Define your type as:

    typedef controlled_vars::limited_fauto_init angle_t;
    

    And when you don't define the CONTROLLED_VARS_DEBUG and CONTROLLED_VARS_LIMITED flags, you get pretty much the same as this:

    typedef float angle_t;
    

    These classes are generated so they include all the necessary operators for you to not suffer too much when using them. That means you can see your angle_t nearly as a float.

    angle_t a;
    a += 35;
    

    Will work as expected (and throw if a + 35 > 360).

    http://snapwebsites.org/project/controlled-vars

    I know this was posted in 2008... but I don't see any good link to a top library that offers this functionality!?

提交回复
热议问题