不允许使用非成员函数重载C ++转换运算符的原因是什么 [英] What is the rationale to not allow overloading of C++ conversions operator with non-member functions

查看:136
本文介绍了不允许使用非成员函数重载C ++转换运算符的原因是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 0x添加了显式转换操作符,但它们必须始终定义为Source类的成员。这同样适用于赋值运算符,它必须在Target类上定义。

C++0x has added explicit conversion operators, but they must always be defined as members of the Source class. The same applies to the assignment operator, it must be defined on the Target class.

当所需转换的Source和Target类彼此独立时,源可以定义一个转换操作符,目标不能从源定义一个构造函数。

When the Source and Target classes of the needed conversion are independent of each other, neither the Source can define a conversion operator, neither the Target can define a constructor from a Source.

通常我们通过定义一个特定的函数,例如

Usually we get it by defining a specific function such as

Target ConvertToTarget(Source& v);

如果C ++ 0x允许通过非成员函数重载转换运算符,我们可以定义转换隐式或显式地在不相关的类型之间。

If C++0x allowed to overload conversion operator by non member functions we could for example define the conversion implicitly or explicitly between unrelated types.

template < typename To, typename From >
operator To(const From& val);



For example we could specialize the conversion from chrono::time_point to posix_time::ptime as follows

template < class Clock, class Duration>
operator boost::posix_time::ptime(
const boost::chrono::time_point<Clock, Duration>& from)
{
  using namespace boost;
  typedef chrono::time_point<Clock, Duration> time_point_t;
  typedef chrono::nanoseconds duration_t;
  typedef duration_t::rep rep_t;
  rep_t d = chrono::duration_cast<duration_t>(
  from.time_since_epoch()).count();
  rep_t sec = d/1000000000;
  rep_t nsec = d%1000000000;
  return  posix_time::from_time_t(0)+
    posix_time::seconds(static_cast<long>(sec))+
    posix_time::nanoseconds(nsec);
}

并将转换用作任何其他转换。

And use the conversion as any other conversion.

有关问题的更完整说明,请参阅此处或我的 Boost.Conversion 媒体库。

For a more complete description of the problem, see here or on my Boost.Conversion library..

所以问题是:不允许使用非成员函数重载C ++转换操作符的原因是什么?

So the question is: What is the rationale to non allow overloading of C++ conversions operator with non-member functions?

推荐答案

使用当前规则,要确定是否可以在两个类之间进行转换,您只需要查看两个位置:源和目标定义。如果您可以将转换定义为非成员函数,则转换函数可能在任何可能导致不必要或模糊转换的原因更困难的地方(除了使编译器更努力地找到可能的转换,需要或可能例如操作员重载)。

With the current rules, to work out whether you can convert between two classes you only need to look in two places: the source and target definitions. If you could define conversions as non-member functions the conversion function could be anywhere which might make finding the cause of unwanted or ambiguous conversions much more difficult (in addition to making the compiler work harder to find possible conversion in all cases where a conversion was need or possible e.g. operator overloading).

我不认为你提出的模板将是非常实用的。虽然您可以明确专门针对转换进行转换,但您有适当的特殊情况,但仍会捕获所有其他转换,从而导致任何预先存在的转换造成歧义。

I don't think that your proposed template would be very practical. Although you could explicitly specialize it for conversion where you did have an appropriate special case, it would still catch all other conversions causing ambiguities with any pre-existing conversions.

或许不允许这种转换的两个潜在因素。

These are perhaps two potential factors in not allowing such conversion.

这篇关于不允许使用非成员函数重载C ++转换运算符的原因是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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