News for hackers. Stuff that matters.
Re:Torvalds: Standards need to be questioned by kazinator on Tuesday June 05, 2018 @06:49PM (#17242779)
What you're missing is that

  *(int *)foo = 0x12345678
is valid C++ and equivalent to using reinterpret_cast. Here, this is the same thing (in C++) as

  *reinterpret_cast<int *>(foo) = 0x12345678
It is not well-defined behavior.

C++ has just divided the behavior of the (TYPE) cast notation in terms of more specific kinds of casts which are mutually exclusive. If a qualifier is being removed, you must use const_cast, which will not change type. The C-style-cast notation automatically chooses the right combination of C++-style casts to do the job. So for instance a C style cast from "const char " to "void ":

  void foo(const void *arg)
  {
    static_cast<char *>(const_cast<void *>(arg));
  }
The same thing can be achieved with just:

  void foo(const void *arg)
  {
    (char *) arg;
  }
So the "bad" thing about the C-style cast notation then is that no matter how you change how the inputs are declared elsewhere, it will just "do what you ask", in the spirit of the C language. Whereas the C++ style casts will diagnose if the input types change so that they are not applicable.

E.g. reinterpret_cast is not so powerful that it will strip away qualifiers:

  void foo(const void *arg)
  {
    reinterpret_cast<char *>(arg); // error: stripped qualifier
  }

There are no comments on this comment yet.