重载c ++ typecasting(函数) [英] Overloading c++ typecasting (functions)

查看:117
本文介绍了重载c ++ typecasting(函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++ style typecastings(all 4)看起来像一些函数模板。例如

  template< typename TO,typename FROM> 
to dynamic_cast(FROM p);

将用作

  dynamic_cast< Derived *>(p); // p is Base * 

为什么不允许重载按语言标准定制使用? (像我们可以重载 new / delete 或其他运算符的关键字

$

$

$ b $ b

我想这是因为标准委员会在介绍这些时,认为所有四个类型的语义都有明确的定义,适用于所有类型是。大多数情况下,这是真的。



我知道的唯一计数器示例是在智能指针实例之间无法 dynamic_cast

  shared_ptr< Derived> pd = dynamic_cast< shared_ptr< Derived> >(pb)。 

我想这样做的能力会有一些优点。



我不知道这是否已经在标准委员会做过所有工作的志愿者讨论过了(我对google太懒了),但如果它已被讨论(我会这样认为),它被拒绝,或者是因为有人认为缺点超过优势,或者因为没有人找到时间提出一个体面的建议,并牧养它。 1






1 不要笑。实际上有很多事情最同意是很好的,而且只是没有实现,因为没有人可以打扰做写作一个体面的提案的工作,花费时间讨论和迭代改进它,直到它可以投票。


Using C++ style typecastings (all 4) look exactly like some function template. e.g.

template<typename TO, typename FROM>
TO dynamic_cast (FROM p);

will be used as,

dynamic_cast<Derived*>(p); // p is Base*

Why is it not allowed to overload them by language standard for custom usage ? (like we can overload the keywords like new/delete or other operators)

解决方案

Why is it not allowed to overload them by language standard for custom usage?

I suppose that's because the standard committee, when introducing these, thought the semantics of all four of these casts are well defined, and applicable to all types they should be. And mostly, this is true.

The only counter example I know of is the inability to dynamic_cast between smart pointer instances:

shared_ptr<Derived> pd = dynamic_cast<shared_ptr<Derived> >(pb);

I suppose the ability to do that would have some merits.

I don't know whether this has been discussed by the volunteers that did all the work in the standards committee (and I'm too lazy to google), but if it has been discussed (and I'd think so), it's been rejected either because someone thought the disadvantages outweigh the advantages, or because nobody had found the time to make a decent proposal and shepherd it through.1


1 Don't laugh. There's actually a lot of things most agree would be nice to have, and which are only failing to materialize because nobody could be bothered to do the work of writing a decent proposal, and spending the time needed to discuss and to iteratively improve it until it can be voted on.

这篇关于重载c ++ typecasting(函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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