Overloading c++ typecasting (functions)

前端 未结 4 1359
长发绾君心
长发绾君心 2021-02-14 16:02

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

template
TO dynamic_cast (         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-14 16:47

    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 pd = dynamic_cast >(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.

提交回复
热议问题