将类型附加到标量的首选机制? [英] preferred mechanism to attach a type to a scalar?

查看:89
本文介绍了将类型附加到标量的首选机制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



将类型附加到标量(例如 double )的最佳方法是什么?典型的用例是计量单位(但我不是要寻找实际的实现, boost有一个)。



这看起来很简单:

  template< typename T> 
struct double final
{
typedef T type;
双重价值;
};

命名空间标签
{
struct foo final {};
struct bar final {};
}
constexpr double FOOS_TO_BARS_ = 3.141592654;
内联Double< tags :: bar> to_bars(const Double< tags :: foo>& foos)
{
return Double< tags :: bar> {foos.value * FOOS_TO_BARS_};
}

静态无效性测试(双值)
{
使用名称空间标记;
const Double< foo> value_in_foos {value};
const Double< bar> value_in_bars = to_bars(value_in_foos);
}

真的是这样吗?还是这种方法存在隐藏的复杂性或其他重要考虑因素?



这似乎远远优于

 内联double foos_to_bars(double foos)
{
return foos * FOOS_TO_BARS_;
}

而几乎不增加任何复杂性或开销。


首先,是的,我认为您所建议的方式是相当合理的,尽管是否首选它取决于上下文。
这种方法的优势在于,您可以定义的转换可能不只是简单的乘法(例如摄氏和华氏度)。



您的方法确实创建了不同的类型,导致需要创建转换,根据用途而定是好是坏。



(感谢您的码和米只是示例,我



如果我正在编写处理长度的代码,那么(大多数)逻辑将是相同的单位。虽然我可以将包含该逻辑的函数作为模板,以便可以采用不同的单元,但是仍然存在合理的用例,其中需要从2个不同来源获取数据并将其提供给不同的单元。在这种情况下,我宁愿处理一个Length类,而不是每个单位一个类,这些长度可以保存其转换信息,也可以只使用一个固定单位,在输入/输出阶段进行转换。



另一方面,当我们有不同类型的不同测量值时,例如长度,面积,温度。在这些类型之间不进行默认转换是一件好事。最好不要在温度上不加长度。



(当然,类型的乘积是不同的。)


[ edit: changed meters/yards to foo/bar; this isn't about converting meters to yards. ]

What's the best way to attach a type to a scalar such as a double? The typical use-case is units-of-measure (but I'm not looking for an actual implementation, boost has one).

This would appear to be a simple as:

template <typename T>
struct Double final
{
    typedef T type;
    double value;
};

namespace tags
{
    struct foo final {};
    struct bar final {};
}
constexpr double FOOS_TO_BARS_ = 3.141592654;
inline Double<tags::bar> to_bars(const Double<tags::foo>& foos)
{
    return Double<tags::bar> { foos.value * FOOS_TO_BARS_ };
}

static void test(double value)
{
    using namespace tags;
    const Double<foo> value_in_foos{ value };    
    const Double<bar> value_in_bars = to_bars(value_in_foos);
}

Is that really the case? Or are there hidden complexities or other important considerations to this approach?

This would seem far, far superior to

   inline double foos_to_bars(double foos)
   {
      return foos * FOOS_TO_BARS_;
   }

without adding hardly any complexity or overhead.

解决方案

Firstly, yes, I think the way you have suggested is quite reasonable, though whether it is to be preferred would depend on the context. Your approach has the advantage that you define conversions that might not just be simple multiplications (example Celsius and Fahrenheit).

Your method however does create different types, which leads to a need to create conversions, this can be good or bad depending on the use.

(I appreciate that your yards and metres were just an example, I'll use it as an just as an example too)

If I'm writing code that deals with lengths, (most of) the logic is going to be the same whatever the units. Whilst I could make the function that contains that logic a template so it can take different units, there's still a reasonable use case where data is needed from 2 different sources and is supplied in to different units. In this situation I'd rather be dealing in one Length class rather than a class per unit, these lengths could either hold their conversion information or it could just use one fixed unit with conversion being done at the input/output stages.

On the other hand when we have different types for different measurements e.g. length, area, temperature. Not having default conversions between these types is a good thing. And it's good that I can't accidently add a length to a temperature.

(Of course multiplication of types is different.)

这篇关于将类型附加到标量的首选机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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