Overloading c++ typecasting (functions)

2019-04-06 17:14发布

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)

3条回答
时光不老,我们不散
2楼-- · 2019-04-06 17:29

I think the reason is the same for you can't overload language keyword.

In fact, you have to see them as language keyword and not template function, even if the look the same. OTOH, I couldn' imagine what kind of disasters one could do by changing the meaning of this particular C++ aspect.

EDIT
I was pretty sure that someone would have come up with the question: "then why you can overload new/delete?". I think that memory allocation/deallocation customization is something that you need in certain scenarios, and the benefits of allowing you to overload them outweighs the risks. I can't see any advantage in subverting the C++ type system, IOW I fail to think a scenario where it would be useful. Do you?

查看更多
smile是对你的礼貌
3楼-- · 2019-04-06 17:35

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.

查看更多
The star\"
4楼-- · 2019-04-06 17:39

Pointer conversion with dynamic_cast, reinterpret_cast and static_cast have well defined meanings and it is probably better not to allow overloading.
It would be a pain to allow users to change the meaning of const_cast.

Only object type casting remains.

struct A
{
  A() {};
  template <typename FROM>
  A(FROM&)  {
    std::cout << "Casting to A \\o/" << std::endl;
  }

  template <typename TO>
  operator TO()  {
    std::cout << "Casting from A \\o/" << std::endl;
    return TO();
  }
};

then

  int i; A a;
  A toA = static_cast<A>(i);  //  Casting to A \o/
  int fromA = static_cast<int>(a); // Casting from A \o/

Hopefully you have better use cases than mine :)

查看更多
登录 后发表回答